XSLT applies to xml document with xmlns attribute

I am applying the XSLT stylesheet to the following XML file:

<top xmlns="http://www.foo.com/bar"> <elementA /> <elementB /> <contents> <contentitem> <id>3</id> <moretags1 /> <moretags2 /> </contentitem> <contentitem> <id>2</id> <moretags1 /> <moretags2 /> </contentitem> <contentitem> <id>1</id> <moretags1 /> <moretags2 /> </contentitem> </contents> </top> 

Here is my current XSLT file (performs a simple look):

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:doc="http://www.foo.com/bar"> <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="contents"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:apply-templates select="contentitem"> <xsl:sort select="id" data-type="number"/> </xsl:apply-templates> </xsl:copy> </xsl:template> </xsl:stylesheet> 

The problem is that I don’t know how to use the doc: 'namespace prefix with xsl: template and xsl: apply-templates tags.

At the moment, the XML document is copied as is, so I believe the first block of the xsl: template template is used. However, the elements are not sorted, so I think the problem is in the second xsl: template.

I should note that if I remove the xmlns attributes from both files, the transformation will work correctly.

Any suggestions?

(question based on this example )

+4
source share
1 answer

Have you tried the prefix element names with the doc: namespace prefix in your selection attributes?

 <xsl:template match="doc:contents"> <xsl:copy> <xsl:apply-templates select="@*"/> <xsl:apply-templates select="doc:contentitem"> <xsl:sort select="doc:id" data-type="number"/> </xsl:apply-templates> </xsl:copy> </xsl:template> 
+9
source

All Articles