using XPATH over namespaces
Questions about XML that are not covered by the other forums should go here.
using XPATH over namespaces
Hi @ll.
I have an XPATH-Problem I am going to lose my hair about ):
In this example:
#XML
#XSLT
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
#XSLT
Please help, this is production critical and I am lost.
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>
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>
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>
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>
If it can't be fixed ... don't break it!
Never mind
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:
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>
If it can't be fixed ... don't break it!
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
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
Return to “General XML Questions”
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service