Strange behavior with xml-namespace

耗尽温柔 提交于 2019-12-02 22:18:41

问题


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:


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>


来源:https://stackoverflow.com/questions/14639918/strange-behavior-with-xml-namespace

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