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

隐身守侯 提交于 2019-11-28 07:12:30

问题


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)


回答1:


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



来源:https://stackoverflow.com/questions/14274166/define-a-default-namespace-for-use-in-xsl-xpaths-with-xpath-default-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!