Page 1 of 1

using XPATH over namespaces

Posted: Tue Aug 09, 2005 3:55 pm
by holli
Hi @ll.

I have an XPATH-Problem I am going to lose my hair about ):

In this example:

#XML

Code: Select all


<?xml version="1.0" encoding="ISO-8859-1"?>
<ehd>
<body>
<gnr_liste>
<gnr V="01100" USE="74" USE-DOMAIN="1.2.276.0.76.5.233" />
<gnr V="01101" USE="74" USE-DOMAIN="1.2.276.0.76.5.233" />
</gnr_liste>
</body>
</ehd>
#XSLT

Code: Select all


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="text" encoding="ISO-8859-1" />

<xsl:template match="/ehd">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="body">
<xsl:text>BOAH</xsl:text>
<xsl:apply-templates />
<xsl:text>!</xsl:text>
</xsl:template>

<xsl:template match="gnr_liste/gnr">
<xsl:text> EY</xsl:text>
</xsl:template>

</xsl:stylesheet>
Here the output is correctly and expectedly: BOAH EY EY!

However in the Real World(tm) case below, the XML contains namespaces and I just can't get the stylesheet to work correctly. It must produce the same output as above, but just outputs "BOAH!".

#XML

Code: Select all


<?xml version="1.0" encoding="ISO-8859-1"?>
<ehd:ehd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:ehd/go/001" xmlns:ehd="urn:ehd/001" ehd_version="1.20">
<ehd:body>
<gnr_liste>
<gnr V="01100" USE="74" USE-DOMAIN="1.2.276.0.76.5.233" />
<gnr V="01101" USE="74" USE-DOMAIN="1.2.276.0.76.5.233" />
</gnr_liste>
</ehd:body>
</ehd:ehd>
#XSLT

Code: Select all


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ehd="urn:ehd/001">
<xsl:strip-space elements="*"/>
<xsl:output method="text" encoding="ISO-8859-1" />

<xsl:template match="/ehd:ehd">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="ehd:body">
<xsl:text>BOAH</xsl:text>
<xsl:apply-templates />
<xsl:text>!</xsl:text>
</xsl:template>

<xsl:template match="gnr_liste/gnr">
<xsl:text> EY</xsl:text>
</xsl:template>

</xsl:stylesheet>
Please help, this is production critical and I am lost.

Never mind

Posted: Tue Aug 09, 2005 4:04 pm
by holli
So, as life goes, a minute after I posted this a colleague comes around the corner and has the solution:

I must add a standard namespace to the stylesheet and use that namespace for elements in the xml that have no namespace. Like so:

Code: Select all


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns="urn:ehd/go/001" xmlns:go="urn:ehd/go/001" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ehd="urn:ehd/001">
<xsl:strip-space elements="*"/>
<xsl:output method="text" encoding="ISO-8859-1" />

<xsl:template match="/ehd:ehd">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="ehd:body">
<xsl:text>BOAH</xsl:text>
<xsl:apply-templates />
<xsl:text>!</xsl:text>
</xsl:template>

<xsl:template match="go:gnr_liste/go:gnr">
<xsl:text> EY</xsl:text>
</xsl:template>

</xsl:stylesheet>

Posted: Tue Aug 09, 2005 4:57 pm
by george
Hi,

Please note that those elements do not have prefix in your source XML but they are in a namespace, they are in the "urn:ehd/go/001" namespace. This is declared as default namespace in the XML file that is why you can enter those elements without a prefix.
In XPath 1.0 if you write an element name without a prefix then that element is considered to be from no namespace so in order to match on those elements from the "urn:ehd/go/001" namespace you must have a prefix mapped to that namespace and use that prefix to qualify the references to elements in that namespace, as in your second example that works.

Hope things are more clear now.

Best Regards,
George