Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
795 views
in Technique[技术] by (71.8m points)

xslt - 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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...