gajus
gajus

Reputation: 15

XSLT filter nodes in certain namespace

I am (very) new to XSLT and was hoping somebody could show me how to filter out certain nodes with XSLT in an XML document depending on the namespace that node is defined in. I have the following simplified example:

Input:

<root 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="xxx:yyy:datamodel:SW:ABCD:01 AB_01.xsd" 
    xmlns="xxx:yyy:datamodel:SW:ABCD:01">   

    <Document xmlns="ABCD:ZYX:01">
        <TypeCode>SEC</TypeCode>
    </Document>
    <Document xmlns="ABCD:NOP:01">
        <TypeCode>DEC</TypeCode>
    </Document> 
</root>

Output:

SEC

So I want only to parse Document nodes in Namespace ABCD:ZYX:01.

This is my xslt so far:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:sec="xxx:yyy:datamodel:ABCD:ZYX:01"
    version="1.0">
    <xsl:template match="sec:TypeCode">
       <xsl:for-each select="//*[local-name() = 'TypeCode']">
            <xsl:value-of select="." />
       </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Thanks in advance!

Upvotes: 0

Views: 295

Answers (1)

Tim C
Tim C

Reputation: 70648

In your XSLT you have defined a namespace of "xxx:yyy:datamodel:ABCD:ZYX:01", but in your XML, the TypeCode you want is in the default namespace of "ABCD:ZYX:01", so your template match in the XSLT will not actually match it.

You probably want to do something like this...

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
                xmlns:sec="ABCD:ZYX:01">
    <xsl:output method="text" />

    <xsl:template match="/">
       <xsl:for-each select="//sec:TypeCode">
            <xsl:value-of select="." />
       </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Related Questions