XSLT与XML源,其默认命名空间设置为xmlns

我有一个XML文档,在根目录中指示一个认的命名空间。
这样的东西:
<MyRoot xmlns="http://www.mysite.com">
   <MyChild1>
       <MyData>1234</MyData> 
   </MyChild1> 
</MyRoot>

解析XML的XSLT不能按预期工作,因为
认命名空间,即当我删除命名空间,一切工作原理
预期。

这里是我的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文档究竟需要做什么?

您需要在XSLT中声明命名空间,并在XPath表达式中使用它。例如。:
<xsl:stylesheet ... xmlns:my="http://www.mysite.com">

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

</xsl:stylesheet>

请注意,如果要在XPath中引用来自该命名空间的元素,则必须提供一些前缀。虽然你可以只使用没有前缀的xmlns =“…”,并且它将用于文字结果元素,但它不适用于XPath – 在XPath中,无前缀的名称始终被认为是在具有空白URI的命名空间中,无论任何xmlns =“…”范围。

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念