是否可以将子节点复制到没有命名空间的输出XML中?

时间:2014-11-05 17:08:51

标签: xml xslt

我正在尝试将soap的所有子元素:Header元素和soap:Body元素复制到一个新元素outputWithoutNS中,同时这样做我在输出XML中看到命名空间,是否可以将子节点复制到输出XML中没有命名空间?

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:foo="http://www.ama.net/1axml-msg/schema/msg-header-1_0.xsd"xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" exclude-result-prefixes="soap x foo">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <xsl:variable name="SOAPHeader" select="/soap:Envelope/soap:Header/node()" />       
        <xsl:variable name="PricingRequest" select="/soap:Envelope/soap:Body/node()" />         
        <xsl:element name="outputWithoutNS">
            <xsl:copy-of select="$SOAPHeader"/> 
            <xsl:copy-of select="$PricingRequest"/> 
        </xsl:element>
    </xsl:template> 
</xsl:stylesheet>

OUTPUT XML getting(MessageHeader和PricingRequest的名称空间):

<outputWithoutNS>
  <MessageHeader xmlns="http://www.ama.net/1axml-msg/schema/msg-header-1_0.xsd" 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 xmlns="http://www.ama.net" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <originatorSection>
      <deliverySystem>
        <companyId>1A</companyId>
        <cityCode>MUC</cityCode>
      </deliverySystem>
    </originatorSection>
  </PricingRequest>
</outputWithoutNS>

期望OUTPUT(MessageHeader和Pricing Request没有名称空间):

<outputWithoutNS>
  <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>
  <PricingRequest>
    <originatorSection>
      <deliverySystem>
        <companyId>1A</companyId>
        <cityCode>MUC</cityCode>
      </deliverySystem>
    </originatorSection>
  </PricingRequest>
</outputWithoutNS>

1 个答案:

答案 0 :(得分:2)

您需要编写模板来剥离名称空间并应用这些模板:

<xsl:stylesheet
  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">

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

<xsl:template match="/">     
    <outputWithoutNS>
        <xsl:apply-templates select="/soap:Envelope/soap:Header/node() | /soap:Envelope/soap:Body/node()"/> 
    </outputWithoutNS>
</xsl:template>