带有XML源的XSLT,其默认名称空间设置为xmlns [英] XSLT with XML source that has a default namespace set to xmlns

查看:165
本文介绍了带有XML源的XSLT,其默认名称空间设置为xmlns的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文档,其根部指示了默认名称空间. 像这样:

I have an XML document with a default namespace indicated at the root. Something like this:

<MyRoot xmlns="http://www.mysite.com">
   <MyChild1>
       <MyData>1234</MyData> 
   </MyChild1> 
</MyRoot>

由于存在以下原因,用于解析XML的XSLT无法正常工作 默认名称空间,即当我删除名称空间时,所有内容都按以下方式工作: 预期的.

The XSLT to parse the XML does not work as expected because of the default namespace, i.e. when I remove the namespace, everything works as expected.

这是我的XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:template match="/" >
  <soap:Envelope xsl:version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
     <NewRoot xmlns="http://wherever.com">
       <NewChild>
         <ChildID>ABCD</ChildID>
         <ChildData>
            <xsl:value-of select="/MyRoot/MyChild1/MyData"/>
         </ChildData>
       </NewChild>
     </NewRoot>
   </soap:Body>
  </soap:Envelope>
 </xsl:template>
</xsl:stylesheet>

使用XSLT文档需要做什么以使翻译正常工作?在XSLT文档中到底需要做什么?

What needs to be done with XSLT document so that translation works properly? What exactly needs to be done in XSLT document?

推荐答案

您需要在XSLT中声明名称空间,并在XPath表达式中使用它.例如:

You need to declare the namespace in your XSLT, and use it in XPath expressions. E.g.:

<xsl:stylesheet ... xmlns:my="http://www.mysite.com">

   <xsl:template match="/my:MyRoot"> ... </xsl:template>

</xsl:stylesheet>

请注意,如果要引用XPath中该命名空间中的元素,则必须提供一些前缀.虽然您可以只使用xmlns="..."而不使用前缀,并且它可以用于文字结果元素,但是不适用于XPath-在XPath中,始终将无前缀名称视为具有空白URI的名称空间,而不管任何xmlns="..."范围.

Note that you must provide some prefix if you want to refer to elements from that namespace in XPath. While you can just do xmlns="..." without the prefix, and it will work for literal result elements, it won't work for XPath - in XPath, an unprefixed name is always considered to be in namespace with blank URI, regardless of any xmlns="..." in scope.

这篇关于带有XML源的XSLT,其默认名称空间设置为xmlns的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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