xmlxsltcharacter-entities

XSL transform an xml with character entities in element names


My xml looks like:

<record>
    <name>ABC</name>
    <address>
        &lt;street&gt;sss&lt;/street&gt;
        &lt;city&gt;ccc&lt;/city&gt;
        &lt;state&gt;ttt&lt;/state&gt;
    </address>
</record>

I am trying to read the element 'street' using the xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output omit-xml-declaration="yes" indent="yes" />
    <xsl:template match="/">
        <xsl:value-of select="record/address/street" />
    </xsl:template>
</xsl:stylesheet>

but it doesn't give any output.

Why does this happen even though the input xml is in a valid xml format? So how to transform xml files containing character entities for element names?


Solution

  • To add to Michael Kay's answer:

    If you start by processing your XML using:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="address">
        <xsl:copy>
            <xsl:value-of select="." disable-output-escaping="yes"/>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    and save the result to file, you will then be able to use your stylesheet to process the resulting file and get the expected result.