Reputation: 105
I have to transform a complex xml to html. Some nodes I can transform, but there are a lot of nodes with unknown meaning. here is simplified xml
<root>
<NodeWithKnownData>
<FirstElement>blah</FirstElement>
<SecondElement>blahBlah</SecondElement>
</NodeWithKnownData>
<NodeWithUnKnownData>
<FirstUnknownElement>blah2134</FirstUnknownElement>
<SecondUnknownElement>blahBlah324523</SecondUnknownElement>
</NodeWithUnKnownData>
<NodeWithRandomNatureData>
<KnownElement>blah2134</KnownElement>
<UnknownElement>blahBlah324523</UnknownElement>
<NewUnknownElement>
<KnownNode2>test</KnownNode2>
<KnownElement>
<KnownNode3>test5654</KnownNode3>
<UnknownNode>test2342345</UnknownNode>
</KnownElement>
</NewUnknownElement>
</NodeWithRandomNatureData>
</root>
I have templates only for known elements. And I have to use my templates and show unknown nodes as "name of node" : "value". Please help me.
Updated
A rule for distinguish known from unknown nodes - only templates for known nodes. if i use this template :
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xhtml" omit-xml-declaration="yes"/>
<xsl:template match="root">
<xsl:apply-templates select="/NodeWithKnownData"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*">
<xsl:value-of select="local-name()"/> : <xsl:value-of select="."/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="NodeWithKnownData">
some useful actions
</xsl:template>
</xsl:stylesheet>
every sub-node is recursevily repeated.
Upvotes: 2
Views: 7949
Reputation: 1
I modified DaveInAZ's answer a bit to return something that looks like the original XML. For my application, I'm trying to develop a custom Win-911 (Win911) notification format, but I didn't know what nodes the XML had to offer.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="*[not(*)]">
<xsl:text><</xsl:text>
<xsl:value-of select="local-name()"/>
<xsl:text>></xsl:text>
<xsl:value-of select="."/>
<xsl:text></</xsl:text>
<xsl:value-of select="local-name()"/>
<xsl:text>></xsl:text>
<br/>
</xsl:template>
<xsl:template match="*[(*)]">
<xsl:text><</xsl:text>
<strong>
<xsl:value-of select="local-name()"/>
</strong>
<xsl:text>></xsl:text>
<br/>
<xsl:apply-templates/>
<xsl:text></</xsl:text>
<strong>
<xsl:value-of select="local-name()"/>
</strong>
<xsl:text>></xsl:text>
<br/>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Reputation: 182
I realize this is an old question/answer, but for anyone looking at it now, the answer given by the OP includes some unnecessary templates that make it more confusing than it has to be. This simplified version produces exactly the same output, without needing to specify any nodenames, known or unknown. The code is the OP's. I don't know if he realized how it actually worked, at the time, but it's actually quite elegant, once the unnecessary bits are stripped away.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xhtml" omit-xml-declaration="yes"/>
<xsl:template match="*[not(*)]">
<xsl:value-of select="local-name()"/> : <xsl:value-of select="."/>
</xsl:template>
<xsl:template match="*[(*)]">
<xsl:value-of select="local-name()"/>
<xsl:apply-templates/>
</xsl:template>
Upvotes: 2