xml 命名空间的奇怪行为 [英] Strange behavior with xml-namespace

查看:26
本文介绍了xml 命名空间的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我得到一个空的输出?当我删除 xml 中的 xmlns 部分时,输出符合预期,但智能感知不再起作用.

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.

仓库.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>

仓库.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>

推荐答案

即使您指定某个命名空间作为 XSL 的默认命名空间,没有命名空间前缀的 XPaths 总是在空命名空间中.你需要做这样的事情:

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>

这篇关于xml 命名空间的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆