需要有关使用基于组的XSLT拆分XML的帮助

时间:2017-06-29 07:40:03

标签: xml xslt-2.0 xslt-grouping

我需要根据XML中不同值的数量将XML拆分为x个文件。

实施例 源XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <MT_RemittanceAdvice>
    <header>
      <companyName>Dept Store</companyName>
      <DocumentType>Remittance Advice</DocumentType>
      <postDate>06/01/2017 03:27:16</postDate>
      <payeeName>xxxxxxxxxxxx</payeeName>
      <checkDate>05/31/2017</checkDate>
      <checkNumber>6321713</checkNumber>
      <region/>
      <amount>13,570.80</amount>
    </header>
    <body>
       <article>
            <transCode/>
            <poNumber>2023090</poNumber>
            <docRef/>
            <site>MY SHOPPINGLANE CEBU, CORP</site>
            <grossAmount>55.80</grossAmount>
            <discount>-0.50</discount>
            <netAmount>-55.30</netAmount>
       </article>
       <article>
            <transCode/>
            <poNumber>210205</poNumber>
            <docRef/>
            <site>ACE HARDWARE PHILS., INC.</site>
            <grossAmount>55.80</grossAmount>
            <discount>-0.50</discount>
            <netAmount>-55.30</netAmount>
        </article>
        <article>
            <transCode/>
            <poNumber>20239479</poNumber>
            <docRef/>
            <site>ACE HARDWARE PHILS., INC.</site>
            <grossAmount>0.00</grossAmount>
            <discount>80.44</discount>
            <netAmount>8,928.62</netAmount>
            </article>
    </body>
    <footer>
           <totalAmount>0.00</totalAmount>
           <ewtAmount>0.00</ewtAmount>
           <arItcc>0.00</arItcc>
           <netPayable>13,570.80</netPayable>
           <importantRemarks><![CDATA[Please review your Details of Payment immediately. Discrepancy noted should be reported within 60 days from credit date. Any request for reconciliation beyond this period shall not be given priority.]]></importantRemarks>
    </footer>

必需结果:使用“site”将单个XML拆分为2个XML。 ACE为1,MY SHOPPING LANE为1。

第一个XML

    <?xml version="1.0" encoding="UTF-8"?>
    <MT_RemittanceAdvice>
    <header>
      <companyName>Dept Store</companyName>
      <DocumentType>Remittance Advice</DocumentType>
      <postDate>06/01/2017 03:27:16</postDate>
      <payeeName>xxxxxxxxxxxx</payeeName>
      <checkDate>05/31/2017</checkDate>
      <checkNumber>6321713</checkNumber>
      <region/>
      <amount>13,570.80</amount>
    </header>
    <body>
       <article>
            <transCode/>
            <poNumber>2023090</poNumber>
            <docRef/>
            <site>MY SHOPPINGLANE CEBU, CORP</site>
            <grossAmount>55.80</grossAmount>
            <discount>-0.50</discount>
            <netAmount>-55.30</netAmount>
       </article>
    </body>
    <footer>
           <totalAmount>0.00</totalAmount>
           <ewtAmount>0.00</ewtAmount>
           <arItcc>0.00</arItcc>
           <netPayable>13,570.80</netPayable>
           <importantRemarks><![CDATA[Please review your Details of Payment immediately. Discrepancy noted should be reported within 60 days from credit date. Any request for reconciliation beyond this period shall not be given priority.]]></importantRemarks>

第二个XML

    <?xml version="1.0" encoding="UTF-8"?>
    <MT_RemittanceAdvice>
    <header>
      <companyName>Dept Store</companyName>
      <DocumentType>Remittance Advice</DocumentType>
      <postDate>06/01/2017 03:27:16</postDate>
      <payeeName>xxxxxxxxxxxx</payeeName>
      <checkDate>05/31/2017</checkDate>
      <checkNumber>6321713</checkNumber>
      <region/>
      <amount>13,570.80</amount>
    </header>
    <body>
       <article>
            <transCode/>
            <poNumber>210205</poNumber>
            <docRef/>
            <site>ACE HARDWARE PHILS., INC.</site>
            <grossAmount>55.80</grossAmount>
            <discount>-0.50</discount>
            <netAmount>-55.30</netAmount>
        </article>
        <article>
            <transCode/>
            <poNumber>20239479</poNumber>
            <docRef/>
            <site>ACE HARDWARE PHILS., INC.</site>
            <grossAmount>0.00</grossAmount>
            <discount>80.44</discount>
            <netAmount>8,928.62</netAmount>
            </article>
    </body>
    <footer>
           <totalAmount>0.00</totalAmount>
           <ewtAmount>0.00</ewtAmount>
           <arItcc>0.00</arItcc>
           <netPayable>13,570.80</netPayable>
           <importantRemarks><![CDATA[Please review your Details of Payment immediately. Discrepancy noted should be reported within 60 days from credit date. Any request for reconciliation beyond this period shall not be given priority.]]></importantRemarks>
    </footer>

页眉和页脚都是从源XML文件中复制的。

以下是我尝试为其创建XSLT:

          <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

<xsl:template match="MT_RemittanceAdvice/header">
    <header>
        <xsl:apply-templates/>
    </header>
</xsl:template>

<xsl:template match="/MT_RemittanceAdvice/body">

    <xsl:for-each-group select="MT_RemittanceAdvice/body/article" group-by="site">
    <body>
        <xsl:result-document method="xml" href="RA_{@ID}-output.xml">
                <xsl:copy-of select="current-group()"/>
        </xsl:result-document>
    </body>
    </xsl:for-each-group>

</xsl:template>

<xsl:template match="MT_RemittanceAdvice/footer">
    <footer>
    <xsl:apply-templates/>
    </footer>
</xsl:template>

1 个答案:

答案 0 :(得分:0)

我不确定您要从哪里获取文件名(输入中没有@ID属性),但假设您可以使用文件索引,则可以使用

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

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

    <xsl:template match="/">
        <xsl:for-each-group select="MT_RemittanceAdvice/body/article" group-by="site">
                <xsl:result-document method="xml" href="RA_{position()}-output.xml">
                    <xsl:apply-templates select="/*"/>
                </xsl:result-document>  
        </xsl:for-each-group>
    </xsl:template>

    <xsl:template match="MT_RemittanceAdvice/body/article">
        <xsl:if test=". intersect current-group()">
            <xsl:next-match/>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

或者当然只是根据需要调整xsl:result-document method="xml" href="RA_{position()}-output.xml"