【问题标题】:XSLT with XML source that has a default namespace set to xmlnsXSLT 与 XML 源的默认名称空间设置为 xmlns
【发布时间】:2010-11-23 13:40:14
【问题描述】:

我有一个 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 文档中究竟需要做什么?

【问题讨论】:

  • ABCD 不是有效的 XSLT。我认为您需要 Control-K 代码。

标签: xslt namespaces


【解决方案1】:

如果您使用 XSLT 2.0,请在 stylesheet 部分中指定 xpath-default-namespace="http://www.example.com"

【讨论】:

    【解决方案2】:

    如果这是一种名称空间问题,则可以尝试修改 xslt 文件中的两件事:

    • 在 xsl:stylesheet 标签中添加“我的”命名空间定义
    • 在遍历xml文件时调用元素时使用“my:”前缀。

    结果

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:my="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="/my:MyRoot/my:MyChild1/my:MyData"/>
                            </ChildData>
                        </NewChild>
                    </NewRoot>
                </soap:Body>
            </soap:Envelope>
        </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

      【解决方案3】:

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

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

      请注意,如果您想在 XPath 中引用来自该命名空间的元素,您必须提供一些前缀。虽然您可以在没有前缀的情况下执行xmlns="...",并且它适用于文字结果元素,但它不适用于 XPath - 在 XPath 中,无论任何 @ 987654324@ 在范围内。

      【讨论】:

      • 感谢您的回答。它类似于我在互联网上找到的,但它不起作用。我的 XML 输出仍然无法按预期工作。如果我从源 XML 中删除默认命名空间,则输出 XML 看起来不错。我进行 XSLT 转换的应用程序是一个 .NET 2.0 应用程序,如果这有所作为的话。
      • 那么请出示您的 XLST 不起作用。没有亲眼所见,很难说出更明确的话。您所描述的内容听起来仍然像是您错过了命名空间限定符 somwhere。例如,请记住,您必须为每个 XPath 步骤重复它 - 即 /my:MyRoot/my:foo/my:bar
      • 关于需要前缀的非常有用的说明。这促使我检查规格。看起来,如果存在默认名称空间,XPath 会尊重默认名称空间,但 XSLT 明确排除默认名称空间在范围内 w3.org/TR/xslt#section-Expressions
      • @PavelMinaev:我在 xPath 中使用了 /my:MyRoot,所以我的模板匹配如下所示:&lt;xsl:template match="/my:MyRoot"&gt; - 那么 MyRoot 是什么 - 是XSLT?
      • 不,它只是一个元素名称。您应该在那里为您的 XML 使用最外层元素的名称。
      猜你喜欢
      • 2012-07-06
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多