I am trying to flatten a hierarchy / structure to XML using XSLT 1 but not having success. – even finding good links…
input xml
<Address addressType="R">
<Structured xmlns="cds_dt">
<Line1>15 Paradise</Line1>
<City>Toronto</City>
<CountrySubdivisionCode>-50</CountrySubdivisionCode>
<PostalZipCode>
<PostalCode>A1A1O1</PostalCode>
</PostalZipCode>
</Structured>
</Address>
desired output xml
<Address addressType="R">
<Formatted xmlns="cds_dt">15 Paradise, Toronto, A1A1O1</Formatted>
</Address>
I tried this .xsl but no luck – error in file
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="cds">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[ancestor::address]">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()[ancestor::address::Structured]">
<xsl:value-of select="concat(',',.)"/>
</xsl:template>
</xsl:stylesheet>
This transformation:
when applied on the provided XML document:
produces the wanted, correct result:
Explanation: Overriding of the identity rule + proper use of namespaces and the
<xsl:element>instruction.