Strange behavior with xml-namespace

2020-05-04 13:58发布

Why do I get an empty output? When I remove the xmlns part in the xml the output is as expected, but then intellisense doesn't work anymore.

warehouse.xml

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="warehouse.xsl"?>
<warehouse xmlns="http://schema.mynamespace.net">
  <container>
    <item>
      <name>book</name>
      <value>1.23</value>
    </item>
    <item>
      <name>phone</name>
      <value>45.6</value>
    </item>
  </container>
</warehouse>

warehouse.xsl

<xsl:stylesheet xmlns="http://schema.mynamespace.net" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:apply-templates select="warehouse/container" />"
</xsl:template>

<xsl:template match="container">
  <xsl:apply-templates />"
</xsl:template>

</xsl:stylesheet>

1条回答
地球回转人心会变
2楼-- · 2020-05-04 14:45

Even if you specify a certain namespace as the default namespace for your XSL, XPaths without a namespace prefix are always in the null namespace. You'll need to do something like this:

<xsl:stylesheet xmlns:mns="http://schema.mynamespace.net" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="text"/>

   <xsl:template match="/">
     <xsl:apply-templates select="mns:warehouse/mns:container" />"
   </xsl:template>

   <xsl:template match="mns:container">
     <xsl:apply-templates />"
   </xsl:template>

</xsl:stylesheet>
查看更多
登录 后发表回答