通过匹配XML标签复制内容

时间:2019-01-28 22:49:21

标签: xml xslt xslt-1.0

我想通过匹配特定节点来复制XML的所有内容,并为每个重复的“ postItemCost”节点添加SOAP信封。

源XML:

    <ns1:postItemCost xmlns:ns1="http://items.test.com/">
      <ItemInvCostReqList>
        <ItemInvCostRequest>
          <invcost>
            <avgcost>0.00</avgcost>
            <lastcost>0.00</lastcost>
            <stdcost>0.00</stdcost>
          </invcost>
          <location>2201</location>
        </ItemInvCostRequest>
        <itemnum>9322979</itemnum>
      </ItemInvCostReqList>
    </ns1:postItemCost>

    <ns1:postItemCost xmlns:ns1="http://items.test.com/">
      <ItemInvCostReqList>
        <ItemInvCostRequest>
          <invcost>
            <avgcost>0.00</avgcost>
            <lastcost>0.00</lastcost>
            <stdcost>0.00</stdcost>
          </invcost>
          <location>1101</location>
        </ItemInvCostRequest>
        <itemnum>9322979</itemnum>
      </ItemInvCostReqList>
    </ns1:postItemCost>

    <ns1:postItemCost xmlns:ns1="http://items.test.com/">
      <ItemInvCostReqList>
        <ItemInvCostRequest>
          <invcost>
            <avgcost>1000.00</avgcost>
            <lastcost>1000.00</lastcost>
            <stdcost>1000.00</stdcost>
          </invcost>
          <location>1101</location>
        </ItemInvCostRequest>
        <itemnum>9322984</itemnum>
      </ItemInvCostReqList>
    </ns1:postItemCost>
  </ns0:Message1>
</ns0:Messages>

预期输出-

<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
  <ns0:Message1>

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body>
    <ns1:postItemCost xmlns:ns1="http://items.test.com/">
      <ItemInvCostReqList>
        <ItemInvCostRequest>
          <invcost>
            <avgcost>0.00</avgcost>
            <lastcost>0.00</lastcost>
            <stdcost>0.00</stdcost>
          </invcost>
          <location>2201</location>
        </ItemInvCostRequest>
        <itemnum>9322979</itemnum>
      </ItemInvCostReqList>
    </ns1:postItemCost>
    </soapenv:Body>
    </soapenv:Envelope>

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body>
    <ns1:postItemCost xmlns:ns1="http://items.test.com/">
      <ItemInvCostReqList>
        <ItemInvCostRequest>
          <invcost>
            <avgcost>0.00</avgcost>
            <lastcost>0.00</lastcost>
            <stdcost>0.00</stdcost>
          </invcost>
          <location>1101</location>
        </ItemInvCostRequest>
        <itemnum>9322979</itemnum>
      </ItemInvCostReqList>
    </ns1:postItemCost>
    </soapenv:Body>
    </soapenv:Envelope>

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body>
    <ns1:postItemCost xmlns:ns1="http://items.test.com/">
      <ItemInvCostReqList>
        <ItemInvCostRequest>
          <invcost>
            <avgcost>1000.00</avgcost>
            <lastcost>1000.00</lastcost>
            <stdcost>1000.00</stdcost>
          </invcost>
          <location>1101</location>
        </ItemInvCostRequest>
        <itemnum>9322984</itemnum>
      </ItemInvCostReqList>
    </ns1:postItemCost>
    </soapenv:Body>
    </soapenv:Envelope>

这是我的XSLT:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="w3.org/1999/XSL/Transform" xmlns:ns0="sap.com/xi/XI/SplitAndMerge" version="1.0">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no" omit-xml-declaration="yes"/>
  <xsl:template match="/">
    <soapenv:Envelope xmlns:soapenv="schemas.xmlsoap.org/soap/envelope">
      <soapenv:Header/>
      <soapenv:Body>
        <xsl:template match="Message//Message1//postInvCostRequest">
          <xsl:copy-of select="/postInvCostRequest"/>
        </xsl:template>
      </soapenv:Body>
    </soapenv:Envelope>
  </xsl:template>
</xsl:stylesheet>

我想为每个重复的“ postItemCost”节点添加SOAP信封。
所以,请您帮我提供XSLT代码,该代码可以帮助我实现预期的输出。

1 个答案:

答案 0 :(得分:0)

这样尝试吗?

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge"
xmlns:ns1="http://items.test.com/">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/ns0:Messages">
    <xsl:copy>
        <ns0:Message1>
            <xsl:for-each select="ns0:Message1/ns1:postItemCost">
                <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                    <soapenv:Header/>
                    <soapenv:Body>
                        <xsl:copy-of select="."/>
                    </soapenv:Body>
                </soapenv:Envelope>
            </xsl:for-each>
        </ns0:Message1>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

演示:http://xsltransform.net/jxWYjVU


已添加:

如注释中所述,您无法控制名称空间声明将出现在何处。以下技巧可用于某些处理器,并强制xmlns:ns1="http://items.test.com/"声明出现在ns1:postItemCost元素上-但不能保证它可与您的每个处理器或序列化组件一起使用处理链:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge"
xmlns:ns3="http://items.test.com/"
exclude-result-prefixes="ns3">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/ns0:Messages">
    <xsl:copy>
        <ns0:Message1>
            <xsl:for-each select="ns0:Message1/ns3:postItemCost">
                <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                    <soapenv:Header/>
                    <soapenv:Body>
                        <xsl:copy-of select="."/>
                    </soapenv:Body>
                </soapenv:Envelope>
            </xsl:for-each>
        </ns0:Message1>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>