2

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.

2
  • What should be the rule for distinguish known from unknown nodes? Do you have xslt templates for all "known" nodes, if so you may add a template match="*" which "trigger to all "other" nodes. Commented Jun 26, 2013 at 20:23
  • I have updated my questions with answer to yourquestion Commented Jun 26, 2013 at 20:45

2 Answers 2

2

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>

Sign up to request clarification or add additional context in comments.

Comments

0

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>&#60;</xsl:text>
        <xsl:value-of select="local-name()"/>
        <xsl:text>&#62;</xsl:text>
        <xsl:value-of select="."/>
        <xsl:text>&#60;/</xsl:text>
        <xsl:value-of select="local-name()"/>
        <xsl:text>&#62;</xsl:text>
        <br/>
    </xsl:template>
    <xsl:template match="*[(*)]">
        <xsl:text>&#60;</xsl:text>
        <strong>
            <xsl:value-of select="local-name()"/>
        </strong>
        <xsl:text>&#62;</xsl:text>
        <br/>
        <xsl:apply-templates/>
        <xsl:text>&#60;/</xsl:text>
        <strong>
            <xsl:value-of select="local-name()"/>
        </strong>
        <xsl:text>&#62;</xsl:text>
        <br/>
    </xsl:template>
</xsl:stylesheet>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.