Is it possible to remove xml attributes from XSLT AND work with the resulting transform?
In other words, I have the following XML:
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="XML_TEST.xslt"?>
<report xmlns="abc123">
<book>
<page id="22">
</page>
<page id="23">
</page>
</book>
</report>
I know that I can use the following XSLT to strip the attributes:
<xsl:template match ="@*" >
<xsl:attribute name ="{local-name()}" >
<xsl:value-of select ="." />
</xsl:attribute>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match ="*" >
<xsl:element name ="{local-name()}" >
<xsl:apply-templates select ="@* | node()" />
</xsl:element>
</xsl:template>
But if I wanted to read the values, using the following template
<xsl:template match="report">
<xsl:for-each select="book/page">
<xsl:value-of select="@id"/>
</xsl:for-each>
</xsl:template>
Could I chain that template to the output of the first two?
Thanks in advance,
-R.
Is it possible to remove xml attributes from XSLT AND work with the resulting transform?
Yes, just search for "multi-pass transformation" and you'll find many answers with good code examples.
However, for what you want to do such transformation chaining is overly complex and completely unnecessary.
Just use:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="abc123" >
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="x:page">
<xsl:value-of select="@id"/>
</xsl:template>
</xsl:stylesheet>
And in case the default namespace of the XML document isn't known in advance, you can still produce the wanted results in a single pass:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="*[name()='page']">
<xsl:value-of select="@id"/>
</xsl:template>
</xsl:stylesheet>