2017-02-08 167 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,是的,你的理解是正确的。非常感谢您的反馈。非常感激。我已经得到了预期的产出。非常感谢。 –