2017-07-05 88 views
0

對於以下輸入XML,我可以使用muenchian grouping方法將所有子元素<invoice>元素分組在相應的<shippingBill>元素中。但是,要求在最終輸出XML中包含<shippingBill>元素的count元素和<invoice>元素。我不知道如何去做。XSLT - 在應用muenchian分組後對子節點進行計數

輸入XML

<bank> 
    <shippingBills> 
     <shippingBill> 
      <shippingBillNo>5786885</shippingBillNo> 
      <shippingBillDate>10/02/2016</shippingBillDate> 
      <LEODate>11/02/2016</LEODate> 
      <invoice> 
       <invoiceSerialNo>1</invoiceSerialNo> 
       <invoiceNo>183</invoiceNo> 
       <invoiceDate>07/02/2016</invoiceDate> 
      </invoice> 
     </shippingBill> 
     <shippingBill> 
      <shippingBillNo>5786885</shippingBillNo> 
      <shippingBillDate>10/02/2016</shippingBillDate> 
      <LEODate>11/02/2016</LEODate> 
      <invoice> 
       <invoiceSerialNo>2</invoiceSerialNo> 
       <invoiceNo>184</invoiceNo> 
       <invoiceDate>07/02/2016</invoiceDate> 
      </invoice> 
     </shippingBill> 
     <shippingBill> 
      <shippingBillNo>3318135</shippingBillNo> 
      <shippingBillDate>01/10/2015</shippingBillDate> 
      <LEODate>01/10/2015</LEODate> 
      <invoice> 
       <invoiceSerialNo>1</invoiceSerialNo> 
       <invoiceNo>172</invoiceNo> 
       <invoiceDate>29/09/2015</invoiceDate> 
      </invoice> 
     </shippingBill> 
     <shippingBill> 
      <shippingBillNo>3318135</shippingBillNo> 
      <shippingBillDate>01/10/2015</shippingBillDate> 
      <LEODate>01/10/2015</LEODate> 
      <invoice> 
       <invoiceSerialNo>2</invoiceSerialNo> 
       <invoiceNo>173</invoiceNo> 
       <invoiceDate>29/09/2015</invoiceDate> 
      </invoice> 
     </shippingBill> 
    </shippingBills> 
</bank> 

XSL

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 
    <xsl:strip-space elements="*"/> 
    <xsl:key name="key-bill" match="shippingBill" use="shippingBillNo"/> 
    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*" /> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="shippingBills"> 
     <xsl:copy> 
      <xsl:apply-templates 
       select="shippingBill[generate-id() = generate-id(key('key-bill', shippingBillNo)[1])]" 
       mode="group" /> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="shippingBill" mode="group"> 
     <shippingBill> 
      <xsl:apply-templates select="*[not(self::invoice)]" /> 
      <invoices> 
       <xsl:apply-templates select="key('key-bill', shippingBillNo)/invoice" /> 
      </invoices> 
     </shippingBill> 
    </xsl:template> 
</xsl:stylesheet> 

最終輸出XML的<checkSum>元件已被添加其具有保持各個C的子元素必需

ounts。

<bank> 
    <checkSum> 
     <noOfInvoices>4</noOfInvoices> 
     <noOfShippingBills>2</noOfShippingBills> 
    </checkSum> 
    <shippingBills> 
     <shippingBill> 
      <shippingBillNo>5786885</shippingBillNo> 
      <shippingBillDate>10/02/2016</shippingBillDate> 
      <LEODate>11/02/2016</LEODate> 
      <invoices> 
       <invoice> 
        <invoiceSerialNo>1</invoiceSerialNo> 
        <invoiceNo>183</invoiceNo> 
        <invoiceDate>07/02/2016</invoiceDate> 
       </invoice> 
       <invoice> 
        <invoiceSerialNo>2</invoiceSerialNo> 
        <invoiceNo>184</invoiceNo> 
        <invoiceDate>07/02/2016</invoiceDate> 
       </invoice> 
      </invoices> 
     </shippingBill> 
     <shippingBill> 
      <shippingBillNo>3318135</shippingBillNo> 
      <shippingBillDate>01/10/2015</shippingBillDate> 
      <LEODate>01/10/2015</LEODate> 
      <invoices> 
       <invoice> 
        <invoiceSerialNo>1</invoiceSerialNo> 
        <invoiceNo>172</invoiceNo> 
        <invoiceDate>29/09/2015</invoiceDate> 
       </invoice> 
       <invoice> 
        <invoiceSerialNo>2</invoiceSerialNo> 
        <invoiceNo>173</invoiceNo> 
        <invoiceDate>29/09/2015</invoiceDate> 
       </invoice> 
      </invoices> 
     </shippingBill> 
    </shippingBills> 
</bank> 

回答

0

我會做這種方式:

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /> 
    <xsl:strip-space elements="*" /> 
    <xsl:key name="key-bill" match="shippingBill" use="shippingBillNo" /> 

    <!-- identity transform --> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()" /> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="shippingBills"> 
     <xsl:variable name="distinct-bills" select="shippingBill[generate-id() = generate-id(key('key-bill', shippingBillNo)[1])]" /> 
     <checkSum> 
      <noOfInvoices> 
       <xsl:value-of select="count(shippingBill/invoice)" /> 
      </noOfInvoices> 
      <noOfShippingBills> 
       <xsl:value-of select="count($distinct-bills)" /> 
      </noOfShippingBills> 
     </checkSum> 
     <xsl:copy> 
      <xsl:apply-templates select="$distinct-bills" mode="group" /> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="shippingBill" mode="group"> 
     <shippingBill> 
      <xsl:apply-templates select="*[not(self::invoice)]" /> 
      <invoices> 
       <xsl:apply-templates select="key('key-bill', shippingBillNo)/invoice" /> 
      </invoices> 
     </shippingBill> 
    </xsl:template> 
</xsl:stylesheet> 
+0

的建議的改變創建的''元件作爲'的'代替子需要''元素的孩子。 ''應該是''的兄弟。 –

+0

@AniketV這是一個微不足道的變化,你應該能夠自己實現它。 –

+0

謝謝。我已經實施了更改,並在更改後的答案中更新了最終的XSL。將答案標記爲已接受。 –

相關問題