2017-02-08 221 views
1

我有這種要求來排序特定的元素。但我不知道我該怎麼做。我嘗試了我見過的XSLT,但無法獲得我想要的預期輸出。輸入XML是這樣的:使用XSLT對元素進行排序

<SyncCredit xmlns="http://schema.XYZ.com/XYZOAGIS/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" releaseID="9.2" versionID="2.12.3"> 
    <Data> 
    <AccountingNo>ERP_12344</AccountingNo> 
    <Credit> 
     <Header> 
     <Name>Supplier Bank</Name> 
     <Date>02-08-2017</Date> 
     </Header> 
     <Payment> 
     <ID>111</ID> 
     <Date>02-01-2017</Date> 
     <Transaction> 
      <BookNo>AA-111</BookNo> 
      <Creditor> 
      <PartyID> 
       <ID schemeName="PassportNo">1000</ID> 
      </PartyID> 
      </Creditor> 
     </Transaction> 
     <Transaction> 
      <BookNo>AA-111</BookNo> 
      <Creditor> 
      <PartyID> 
       <ID schemeName="TaxIdentificationNo">5000</ID> 
      </PartyID> 
      </Creditor> 
     </Transaction> 
     </Payment> 
     <Payment> 
     <ID>222</ID> 
     <Date>02-05-2017</Date> 
     <Transaction> 
      <BookNo>AA-111</BookNo> 
      <Creditor> 
      <PartyID> 
       <ID schemeName="BankPartyNo">4000</ID> 
      </PartyID> 
      </Creditor> 
     </Transaction> 
     <Transaction> 
      <BookNo>AA-111</BookNo> 
      <Creditor> 
      <PartyID> 
       <ID schemeName="DriverLicense">2000</ID> 
      </PartyID> 
      </Creditor> 
     </Transaction> 
     </Payment> 
    </Credit> 
    </Data> 
</SyncCredit> 

我預期的輸出應該是這樣的:

<SyncCredit xmlns="http://schema.XYZ.com/XYZOAGIS/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" releaseID="9.2" versionID="2.12.3"> 
    <Data> 
    <AccountingNo>ERP_12344</AccountingNo> 
    <Credit> 
     <Header> 
     <Name>Supplier Bank</Name> 
     <Date>02-08-2017</Date> 
     </Header> 
     <Payment> 
     <ID>111</ID> 
     <Date>02-01-2017</Date> 
     <Transaction> 
      <BookNo>AA-111</BookNo> 
      <Creditor> 
      <PartyID> 
       <ID schemeName="PassportNo">1000</ID> 
      </PartyID> 
      </Creditor> 
     </Transaction> 
     </Payment> 
     <Payment> 
     <ID>222</ID> 
     <Date>02-05-2017</Date> 
     <Transaction> 
      <BookNo>AA-111</BookNo> 
      <Creditor> 
      <PartyID> 
       <ID schemeName="DriverLicense">2000</ID> 
      </PartyID> 
      </Creditor> 
     </Transaction> 
     </Payment> 
     <Payment> 
     <ID>222</ID> 
     <Date>02-05-2017</Date> 
     <Transaction> 
      <BookNo>AA-111</BookNo> 
      <Creditor> 
      <PartyID> 
       <ID schemeName="BankPartyNo">4000</ID> 
      </PartyID> 
      </Creditor> 
     </Transaction> 
     </Payment> 
     <Payment> 
     <ID>111</ID> 
     <Date>02-01-2017</Date> 
     <Transaction> 
      <BookNo>AA-111</BookNo> 
      <Creditor> 
      <PartyID> 
       <ID schemeName="TaxIdentificationNo">5000</ID> 
      </PartyID> 
      </Creditor> 
     </Transaction> 
     </Payment> 
    </Credit> 
    </Data> 
</SyncCredit> 

債權人/ PartyID/ID元素具有同時保持其他元素原封不動進行排序。但是,我需要將排序後的Creditor/PartyID/ID拆分爲每個<Payment>節點。我正在使用XSLT v2.0。雖然,我能夠給債權人排序使用<xsl:perform-sort>從我得到的參考,輸出我得到妥善/ PartyID/ID是這樣的:

<Payment> 
    <Transaction xmlns="http://schema.XYZ.com/XYZOAGIS/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <BookNo>AA-111</BookNo> 
    <Creditor> 
     <PartyID> 
     <ID schemeName="PassportNo">1000</ID> 
     </PartyID> 
    </Creditor> 
    </Transaction> 
    <Transaction xmlns="http://schema.XYZ.com/XYZOAGIS/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <BookNo>AA-111</BookNo> 
    <Creditor> 
     <PartyID> 
     <ID schemeName="DriverLicense">2000</ID> 
     </PartyID> 
    </Creditor> 
    </Transaction> 
    <Transaction xmlns="http://schema.XYZ.com/XYZOAGIS/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <BookNo>AA-111</BookNo> 
    <Creditor> 
     <PartyID> 
     <ID schemeName="BankPartyNo">4000</ID> 
     </PartyID> 
    </Creditor> 
    </Transaction> 
    <Transaction xmlns="http://schema.XYZ.com/XYZOAGIS/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <BookNo>AA-111</BookNo> 
    <Creditor> 
     <PartyID> 
     <ID schemeName="TaxIdentificationNo">5000</ID> 
     </PartyID> 
    </Creditor> 
    </Transaction> 

這是我的XSLT,它是不完整的,因爲我不知道如何複製Header下的元素和Payment.I只嘗試在Transaction元素中執行XSLT。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xpath-default-namespace="http://schema.XYZ.com/XYZOAGIS/2"> 
<xsl:output method="xml" omit-xml-declaration="yes" indent="no"/> 
<xsl:template match="/"> 
    <xsl:variable name="Transaction"> 
     <xsl:perform-sort select="SyncCredit/Data/Credit/Payment/Transaction"> 
      <xsl:sort data-type="number" select="Creditor/PartyID/ID"/> 
     </xsl:perform-sort> 
    </xsl:variable> 
    <Payment> 
     <xsl:copy-of select="$Transaction"/> 
    </Payment> 
</xsl:template> 

預先感謝您。

+0

告訴我們,你已經嘗試了XSLT。 –

+0

問題不明確。請注意,您的輸出顯示與輸入不同的_structure_。在輸入中,每個''包含_two_' ,但是在預期的輸出中,每個事務已被分解到它自己的Payment節點中。 –

+0

@Jim Garrison:謝謝。我已經編輯了我的文章,幷包含了我創建的XSLT。是的,你是對的。在輸入中,付款標籤包含2個交易標籤。我需要的是對Creditor/PartyID/ID進行排序,並且我需要將其分解到它自己的Payment節點中。 –

回答

0

如果我理解正確的這個,你想做的事:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xpath-default-namespace="http://schema.XYZ.com/XYZOAGIS/2" 
xmlns="http://schema.XYZ.com/XYZOAGIS/2"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="Credit"> 
    <xsl:copy> 
     <xsl:copy-of select="Header"/> 
     <xsl:apply-templates select="Payment/Transaction"> 
      <xsl:sort select="Creditor/PartyID/ID" data-type="number" order="ascending"/> 
     </xsl:apply-templates> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Transaction"> 
    <Payment> 
     <xsl:copy-of select="../ID, ../Date, ."/> 
    </Payment> 
</xsl:template> 

</xsl:stylesheet> 
+0

嗨@ michael.hor257k,是的,你的理解是正確的。非常感謝您的反饋。非常感激。我已經得到了預期的產出。非常感謝。 –