XSL复制祖先节点的属性,而我只需要来自上下文节点的属性

时间:2016-12-09 18:00:38

标签: xslt

我正在复制Body的后代树。在输出中,如何禁止除子节点中的xmlns =“https://www.google.com/apis/ads/publisher/v201605”之外的所有属性。

这是输入XML

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
        xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Header>
    <ns1:RequestHeader
         soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next"
         soapenv:mustUnderstand="0"
         xmlns:ns1="https://www.google.com/apis/ads/publisher/v201605">
      <ns1:networkCode>123456</ns1:networkCode>
      <ns1:applicationName>DfpApi-Java-2.1.0-dfp_test</ns1:applicationName>
    </ns1:RequestHeader>
  </soapenv:Header>
  <soapenv:Body>
    <getAdUnitsByStatement xmlns="https://www.google.com/apis/ads/publisher/v201605">
      <filterStatement>
        <query>WHERE parentId IS NULL LIMIT 500</query>
      </filterStatement>
    </getAdUnitsByStatement>
  </soapenv:Body>
</soapenv:Envelope>

这是XSL

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@* | node()">
        <xsl:copy-of select=".//*:Body/* " />  
    </xsl:template>
</xsl:stylesheet>

这是输出:

<getAdUnitsByStatement xmlns="https://www.google.com/apis/ads/publisher/v201605"
              xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
              xmlns:xsd="http://www.w3.org/2001/XMLSchema"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <filterStatement>
    <query>WHERE parentId IS NULL LIMIT 500</query>
    </filterStatement>
  </getAdUnitsByStatement>

感谢。

和Sandeep

1 个答案:

答案 0 :(得分:1)

假设您确实使用了XSLT 3.0处理器或至少使用了XSLT 2.0处理器,那么请替换

<xsl:template match="@* | node()">
    <xsl:copy-of select=".//*:Body/* " />  
</xsl:template>

<xsl:template match="/">
    <xsl:copy-of select="//*:Body/* " copy-namespaces="no"/>  
</xsl:template>