Faqts : Computers : Programming : Languages : XPath

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

12 of 14 people (86%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How do you match elements in the default namespace (e.g. xmlns="uri") in XPath 2.0?
How do you match elements in the default namespace (e.g. xmlns="uri") in XPath 2.0 within XSLT 2.0?
How do you match elements in the default namespace (e.g. xmlns="uri") in XPath 2.0 within XQuery 1.0

Mar 2nd, 2005 04:11
Martin Honnen, http://www.w3.org/TR/xslt20/#unprefixed-qnames http://www.w3.org/TR/xquery/#id-default-namespace


XML with namespaces allows you to use a so called default namespace on
elements, see
  <http://www.w3.org/TR/REC-xml-names/#ns-decl>
  <http://www.w3.org/TR/REC-xml-names/#defaulting>
where you use an attribute with name 'xmlns' and the value being the
namespace URI e.g.
  <element xmlns="http://example.com/2005/02/ex1" />
to use elements in a certain namespace without the need to bind a prefix
to that namespace.
As <http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/1753>
explains with XPath 1.0 it is not possible to select/match elements in
the default namespace with an XPath expression without binding a prefix
to that namespace URI and use that prefix in the expression.
With XPath 2.0 however you can select/match elements in the default
namespace without the need to bind a prefix to the namespace URI.
If XPath 2.0 is used within XSLT 2.0 you can use the special attribute
[xsl:]xpath-default-namespace in your stylesheet to set the default
namespace for XPath expressions, see
<http://www.w3.org/TR/xslt20/#unprefixed-qnames> for details.
On XSLT instruction elements (e.g. xsl:value-of) you use the attribute
without prefix (e.g. <xsl:value-of xpath-default-namespace="uri"
select="*" />), on other elements you use the attribute within the XSLT
namespace (e.g. <result-element xsl:xpath-default-namespace="uri">).
Thus with the following XML input
<element xmlns="http://example.com/2005/03/ns1" />
and then the following XSLT 2.0 stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
<xsl:output method="xml" indent="yes" encoding="UTF-8" />
<xsl:template match="/">
  <results>
    <result xpath-expression="count(//element)">
      <xsl:value-of select="count(//element)" />
    </result>
    <result xsl:xpath-default-namespace="http://example.com/2005/03/ns1"
            xpath-expression="count(//element)">
      <xsl:value-of select="count(//element)" />
    </result>
    <result xpath-expression="count(//element)">
      <xsl:value-of xpath-default-namespace="http://example.com/2005/03/ns1"
                    select="count(//element)" />
    </result>
  </results>
</xsl:template>
</xsl:stylesheet>
the transformation result is
<?xml version="1.0" encoding="UTF-8"?>
<results>
   <result xpath-expression="count(//element)">0</result>
   <result xpath-expression="count(//element)">1</result>
   <result xpath-expression="count(//element)">1</result>
</results>
If you use XPath 2.0 within XQuery 1.0 then you can declare the default
namespace as follows:
declare default element namespace "uri";
see <http://www.w3.org/TR/xquery/#id-default-namespace> for details.
Using the above given example XML input the following XQuery 1.0 program:
declare default element namespace "http://example.com/2005/03/ns1";
let $n := count(//element) return
<results xmlns="">
  <result xpath-expression="count(//element)">
    { $n }
  </result>
</results>
outputs 
<?xml version="1.0" encoding="UTF-8"?>
<results>
   <result xpath-expression="count(//element)">1</result>
</results>