XSLT使用namespaced xpath

时间:2017-04-04 13:29:54

标签: xml xslt xpath

使用question1question2我能够使用非命名空间XML上的XSLT基于属性值删除Element的值 但我的输入看起来像这样

<tns:message xmlns:tns="http://www.co.com/schemas/sys">
   <Body xmlns="http://co.com/message">
      <Record xmlns="http://schemas.co.com/Record/1.0">
         <Book Class="NOVEL">Jungle Book</Book>
         <Book Class="AUTOBIOGRAPHY">The Autobiography of Benjamin Franklin</Book>
      </Record>
   </Body>
</tns:message>

我曾尝试使用XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msg="http://co.com/message"
xmlns:rec="http://schemas.co.com/Record/1.0">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="msg:Body/rec:Record/Book[@Class = 'NOVEL']/text()" />
</xsl:stylesheet>

但没有运气。
当我将命名空间前缀添加到输入中时,它已经起作用 提前谢谢。

1 个答案:

答案 0 :(得分:1)

继承默认名称空间(不带前缀)。这意味着输入中的Book元素与其父Record位于同一名称空间中 - 您需要更改此内容:

<xsl:template match="msg:Body/rec:Record/Book[@Class = 'NOVEL']/text()" />

为:

<xsl:template match="msg:Body/rec:Record/rec:Book[@Class = 'NOVEL']/text()" />
相关问题