Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a default namespace for use in XSL XPaths with xpath-default-namespace

I have this simple xml document:

<?xml version='1.0' encoding='UTF-8'?>
<registry xmlns="http://www.iana.org/assignments" id="character-sets">
     <registry id="character-sets-1">
       <record>
         <name>ANSI_X3.4-1968</name>
      </record>
     </registry>
</registry>

When I use this xsl I can extract the name:

<?xml version="1.0" encoding="UTF-8"?>
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://www.iana.org/assignments" version="1.0">
  <xsl:template match="/my:registry">
      <xsl:copy-of select="//my:record/my:name"/>
  </xsl:template>
</xsl:stylesheet>

However, If I omit the namespace in the xsl xpath-selectors, I get no output:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://www.iana.org/assignments" xpath-default-namespace="http://www.iana.org/assignments" version="1.0">
  <xsl:template match="/registry">
       <xsl:copy-of select="//record/name"/>
  </xsl:template>
</xsl:stylesheet>

I thought xpath-default-namespace is meant to do the trick. What am I missing?

In case library versions are important I have

libexpat1 (>= 1.95.8)

libxerces-c3.1

libxml2 (>= 2.7.4)

libxslt1.1 (>= 1.1.25)

like image 888
JohnDoe Avatar asked Jan 11 '13 08:01

JohnDoe


1 Answers

Unfortunately xpath-default-namespace is an XSLT 2.0 feature. You'll need to repeat the namespace or alias it in your xpath in xslt 1.0

Reference : Jenni Tennison and IBM

like image 192
StuartLC Avatar answered Oct 22 '22 22:10

StuartLC