删除子节点名称空间并将另一个节点复制到变量中

时间:2014-11-04 23:16:56

标签: xslt xslt-1.0

我正在尝试将soap的所有子元素:Header元素复制到变量中,并尝试删除PricingRequest空命名空间(xmlns =" http://www.ama.net")并输出只有PriceRequest部分xml。我能够删除PricingRequest空命名空间并获得正确的输出但不能将soap:Header子元素存储到变量中,是否可以将soap:Header的子元素复制到变量中?

INPUT XML:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap ="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header xmlns="http://www.ama.net/1axml-msg/schema/msg-header-1_0.xsd">
  <MessageHeader ResponseRequested="true" version="1.0" Terminate="true" Reverse="true" id="09B5581A" soap:mustUnderstand="1">
    <From>1ASI</From>
    <To>1ASRINSAIG</To>
    <TimeStamp>
      <GenerationTime>2014-10-22T12:41:38Z</GenerationTime>
    </TimeStamp>    
  </MessageHeader>
</soap:Header>
<soap:Body>
<PricingRequest xmlns="http://www.ama.net">
    <originatorSection>
        <deliverySystem>
            <companyId>1A</companyId>
            <cityCode>MUC</cityCode>
        </deliverySystem>       
    </originatorSection>    
</PricingRequest>
</soap:Body>
</soap:Envelope>

XSLT:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://www.ama.net" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
<!--    <xsl:template match="/*[local-name()='Envelope']">
        <xsl:variable name="SOAPHeader" select="/soap:Header/node()" />
        <xsl:text> MessageHeader INFO:<xsl:copy-of select="$SOAPHeader"/>  </xsl:text>
    </xsl:template>  -->

    <xsl:template match="*">
        <xsl:element name="{name()}">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:element>      
    </xsl:template>
    <xsl:template match="*[not(ancestor-or-self::x:PricingRequest)]">
        <xsl:apply-templates/>
    </xsl:template>

</xsl:stylesheet>

期望输出但不能将soap:Header复制到变量中:

<PricingRequest>
  <originatorSection>
    <deliverySystem>
      <companyId>1A</companyId>
      <cityCode>MUC</cityCode>
    </deliverySystem>
  </originatorSection>
</PricingRequest>

2 个答案:

答案 0 :(得分:1)

尝试;

<xsl:template match="/">
    <xsl:variable name="SOAPHeader" select="/soap:Envelope/soap:Header/node()" />
    <xsl:text>MessageHeader INFO:</xsl:text>
    <xsl:copy-of select="$SOAPHeader"/>  
</xsl:template>

注意

  1. <xsl:text>不能包含子元素;
  2. 您的版本有soap:Envelope的冲突模板。

答案 1 :(得分:0)

以下代码按我想要的方式工作。我能够复制soap:变量中的Header子元素,并在输出中删除了PricingRequest命名空间

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.ama.net/1axml-msg/schema/msg-header-1_0.xsd"
    xmlns:ns2="http://www.ama.net" exclude-result-prefixes="ns1 ns2 soap">

    <xsl:template match="ns1:* | ns2:*| soap:*">
        <xsl:element name="{local-name()}">
            <xsl:copy-of select="@*" />
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>

    <xsl:template match="/">
        <xsl:variable name="SOAPHeader">
            <xsl:apply-templates select="/soap:Envelope/soap:Header/node()" />          
        </xsl:variable>         
        <xsl:text>HeaderInfo:</xsl:text>
        <xsl:copy-of select="$SOAPHeader" />
        <xsl:apply-templates select="/soap:Envelope/soap:Body/node()" />    
    </xsl:template>

</xsl:stylesheet>

我得到的输出:

<?xml version="1.0"?>
HeaderInfo:
  <MessageHeader xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" ResponseRequested="true" version="1.0" Terminate="true" Reverse="true" id="09B5581A" soap:mustUnderstand="1">
    <From>1ASI</From>
    <To>1ASRINSAIG</To>
    <TimeStamp>
      <GenerationTime>2014-10-22T12:41:38Z</GenerationTime>
    </TimeStamp>    
  </MessageHeader>

<PricingRequest>
    <originatorSection>
        <deliverySystem>
            <companyId>1A</companyId>
            <cityCode>MUC</cityCode>
        </deliverySystem>       
    </originatorSection>    
</PricingRequest>