使用XSLT添加新的父节点

时间:2019-05-17 05:12:13

标签: xml xslt xpath

我有一个简单的XML,如下所示

    <?xml version="1.0" encoding="UTF-8"?>
    <StockLevelReportingExt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <Envelope>
          <SenderIdentification>USLAX19</SenderIdentification>
       </Envelope>
       <Message>
          <ArticleInformation>
             <SmallNormalID>2</SmallNormalID>
             <SerialNumbersPerSKU>0</SerialNumbersPerSKU>
          </ArticleInformation>
          <SubtotalsInformation>
             <TaxedFlag>0</TaxedFlag>
             <Depot>U15</Depot>
          </SubtotalsInformation>
       </Message>
       <Message>
          <ArticleInformation>
             <SmallNormalID>2</SmallNormalID>
             <SerialNumbersPerSKU>0</SerialNumbersPerSKU>
          </ArticleInformation>
          <SubtotalsInformation>
             <TaxedFlag>0</TaxedFlag>
             <Depot>U15</Depot>
          </SubtotalsInformation>
       </Message>
    </StockLevelReportingExt>

我需要添加一个父节点,将所有<Messages>包裹在一个<Messages>父节点下。

因此生成的xml应该如下所示,

<?xml version="1.0" encoding="UTF-8"?>
        <StockLevelReportingExt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
           <Envelope>
              <SenderIdentification>USLAX19</SenderIdentification>
           </Envelope>
           <Messages> // Newly added node
              <Message>
                 <ArticleInformation>
                    <SmallNormalID>2</SmallNormalID>
                    <SerialNumbersPerSKU>0</SerialNumbersPerSKU>
                  </ArticleInformation>
                 <SubtotalsInformation>
                    <TaxedFlag>0</TaxedFlag>
                    <Depot>U15</Depot>
                 </SubtotalsInformation>
              </Message>
              <Message>
                 <ArticleInformation>
                    <SmallNormalID>2</SmallNormalID>
                    <SerialNumbersPerSKU>0</SerialNumbersPerSKU>
                 </ArticleInformation>
                 <SubtotalsInformation>
                    <TaxedFlag>0</TaxedFlag>
                    <Depot>U15</Depot>
                 </SubtotalsInformation>
              </Message>
           </Messages>   
        </StockLevelReportingExt>

如何获得预期的结果?我尝试了以下代码将父节点添加到<Message>,但是它不起作用。

<?xml version="1.0"?>
<xsl:stylesheet xmlns:ifs="urn:ifsworld-com:schemas:TYPE_SCHEMA_NAME_HERE" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://exslt.org/dates-and-times" version="1.0" exclude-result-prefixes="ifs date">
<xsl:output method="xml" indent="yes"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Message">
    <xsl:copy>
      <xsl:element name="Messages"/>
      <xsl:apply-templates select="@* | *"/> 
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

这可以做到:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:strip-space elements="*"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="StockLevelReportingExt">
    <xsl:copy>
      <xsl:apply-templates select="@* |node()[not(self::Message)]"/>
      <Messages>
        <xsl:apply-templates select="Message"/>
      </Messages>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

输出:

<?xml version="1.0" encoding="utf-8"?>
<StockLevelReportingExt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Envelope>
    <SenderIdentification>USLAX19</SenderIdentification>
  </Envelope>
  <Messages>
    <Message>
      <ArticleInformation>
        <SmallNormalID>2</SmallNormalID>
        <SerialNumbersPerSKU>0</SerialNumbersPerSKU>
      </ArticleInformation>
      <SubtotalsInformation>
        <TaxedFlag>0</TaxedFlag>
        <Depot>U15</Depot>
      </SubtotalsInformation>
    </Message>
    <Message>
      <ArticleInformation>
        <SmallNormalID>2</SmallNormalID>
        <SerialNumbersPerSKU>0</SerialNumbersPerSKU>
      </ArticleInformation>
      <SubtotalsInformation>
        <TaxedFlag>0</TaxedFlag>
        <Depot>U15</Depot>
      </SubtotalsInformation>
    </Message>
  </Messages>
</StockLevelReportingExt>
相关问题