使用XSLT进行XML转换,需要从多个复杂类型中获取max expirationdate

时间:2013-01-27 03:12:49

标签: xml xslt-1.0

我有以下节点结构,我需要在XSLT中的所有<FTACERTEXPIRATIONDATE>中获取最大<partSupplierFTA>。有人可以帮帮我吗?

<modelMasterHeader>
   <sender>sender3889</sender>
   <receiver>receiver3890</receiver>
   <messageType>messageType3891</messageType>
   <batchID>97</batchID>
   <modelMaster>
      <ACTION>M</ACTION>
      <PARTNUMBER>PARTNUMBER3893</PARTNUMBER>
      <productCountry>
         <COUNTRYCODE>COUNTRYCODE3894</COUNTRYCODE>
         <COUNTRYGROUP>COUNTRYGROUP3895</COUNTRYGROUP>
         <LOCALPARTDESCRIPTION>LOCALPARTDESCRIPTION3896</LOCALPARTDESCRIPTION>
         <COUNTRYOFORIGIN>COUNTRYOFORIGIN3897</COUNTRYOFORIGIN>
         <HS>-0</HS>
         <HSDESCRIPTION>HSDESCRIPTION3898</HSDESCRIPTION>
         <SOURCESYSTEM>SOURCESYSTEM3899</SOURCESYSTEM>
         <ECCNUM>ECCNUM3900</ECCNUM>
         <USEGENLIC>USEGENLIC3901</USEGENLIC>
         <EXPORTHARMONIZEDTARIFFNUMBER>-0</EXPORTHARMONIZEDTARIFFNUMBER>
         <PARTUNIT>PARTUNIT3902</PARTUNIT>
      </productCountry>
      <Supplier>
         <PARTSUPPCOUNTRYOFORIGIN></PARTSUPPCOUNTRYOFORIGIN>
         <PRIMARYFLAG>Y</PRIMARYFLAG>
         <CTRY_OF_IMPORT>US</CTRY_OF_IMPORT>
         <SEQ_NO>23</SEQ_NO>
         <SupplierFTA>
            <FTASITEID>GLOBAL</FTASITEID>
            <FTASEQNO2>1</FTASEQNO2>
            <FTACERTEXPIRATIONDATE>11/30/2012</FTACERTEXPIRATIONDATE>
            <FTACOUNTRYOFORIGIN>US</FTACOUNTRYOFORIGIN>
            <FTASTATUS>H</FTASTATUS>
            <FTAHTSNUMBER>8414593000</FTAHTSNUMBER>
         </SupplierFTA>
         <SupplierFTA>
            <FTASITEID>GLOBAL</FTASITEID>
            <FTASEQNO2>2</FTASEQNO2>
            <FTACERTEXPIRATIONDATE>12/31/2013</FTACERTEXPIRATIONDATE>
            <FTACOUNTRYOFORIGIN>CN</FTACOUNTRYOFORIGIN>
            <FTASTATUS>V</FTASTATUS>
            <FTAHTSNUMBER>8414593000</FTAHTSNUMBER>
         </SupplierFTA>
         <SupplierFTA>
            <FTASITEID>GLOBAL</FTASITEID>
            <FTASEQNO2>1</FTASEQNO2>
            <FTACERTEXPIRATIONDATE>12/31/2012</FTACERTEXPIRATIONDATE>
            <FTACOUNTRYOFORIGIN>AU</FTACOUNTRYOFORIGIN>
            <FTASTATUS>V</FTASTATUS>
            <FTAHTSNUMBER>8302103000</FTAHTSNUMBER>
         </SupplierFTA>
        </Supplier>
        <Supplier>
         <PARTSUPPCOUNTRYOFORIGIN></PARTSUPPCOUNTRYOFORIGIN>
         <PRIMARYFLAG>Y</PRIMARYFLAG>
         <CTRY_OF_IMPORT>US</CTRY_OF_IMPORT>
         <SEQ_NO>23</SEQ_NO>
         <SupplierFTA>
            <FTASITEID>GLOBAL</FTASITEID>
            <FTASEQNO2>1</FTASEQNO2>
            <FTACERTEXPIRATIONDATE>11/30/2012</FTACERTEXPIRATIONDATE>
            <FTACOUNTRYOFORIGIN>US</FTACOUNTRYOFORIGIN>
            <FTASTATUS>H</FTASTATUS>
            <FTAHTSNUMBER>8414593000</FTAHTSNUMBER>
         </SupplierFTA>
         <SupplierFTA>
            <FTASITEID>GLOBAL</FTASITEID>
            <FTASEQNO2>2</FTASEQNO2>
            <FTACERTEXPIRATIONDATE>12/31/2013</FTACERTEXPIRATIONDATE>
            <FTACOUNTRYOFORIGIN>CN</FTACOUNTRYOFORIGIN>
            <FTASTATUS>V</FTASTATUS>
            <FTAHTSNUMBER>8414593000</FTAHTSNUMBER>
         </SupplierFTA>
         <SupplierFTA>
            <FTASITEID>GLOBAL</FTASITEID>
            <FTASEQNO2>1</FTASEQNO2>
            <FTACERTEXPIRATIONDATE>12/31/2012</FTACERTEXPIRATIONDATE>
            <FTACOUNTRYOFORIGIN>AU</FTACOUNTRYOFORIGIN>
            <FTASTATUS>V</FTASTATUS>
            <FTAHTSNUMBER>8302103000</FTAHTSNUMBER>
         </SupplierFTA>
        </Supplier>
     </ModelMaster>
   </ModelMasterHeader>

2 个答案:

答案 0 :(得分:1)

这将使用XSLT2找到最新日期。如果您需要找到包含<SupplierFTA>节点,这应该为您提供一个起点。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version="2.0">
    <xsl:variable name="a" select="//FTACERTEXPIRATIONDATE/text()"/>
    <xsl:template match="root">
        <xsl:value-of select="max(for $x in $a return xs:date(replace($x,'^([0-9]+)/([0-9]+)/([0-9]+)$','$3-$1-$2')))"/>
    </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

虽然效率很低,但您可以使用xsl:for-each并按年,月和日对值进行排序,然后选择第一项:

<xsl:for-each select="
        /modelMasterHeader/modelMaster/Supplier/SupplierFTA/FTACERTEXPIRATIONDATE
        ">
    <xsl:sort order="descending" data-type="number" 
        select="substring-after(substring-after(., '/'), '/')"/>
    <xsl:sort order="descending" data-type="number" 
        select="substring-before(substring-after(., '/'), '/')"/>
    <xsl:sort order="descending" data-type="number" 
        select="substring-before(.,'/')"/>
    <xsl:if test="position()=1">
        <xsl:value-of select="."/>
    </xsl:if>
</xsl:for-each>