2017-01-06 7 views
0

車のモデル、価格、月払い価格のリストを含む比較的大きなXMLファイルがあります。実際にそこに他の情報がたくさんありますが、それは私が興味を持っている重要なデータです。XSLT 1.0を使用してグループ化されたアイテムを並べ替える

そこには重複したモデルがいくつかあります。これまで私がこのフォーラムの助けを借りて行ってきた私の仕事は、そのモデルの中で最も安価な車両を示すモデル(IEは重複しない)の別個のリストを作ることです。

私が立ち往生しているビットは、最も低い月々の支払いを最も高い月々の支払い額に表示するこのリストをソートする必要があります。最も安価な車両であるという合併症は、最低限の月額支払いとは必ずしも同じではありません。

<?xml version="1.0" encoding="utf-8"?> 
<Dealer> 
    <Vehicle> 
     <Model>KA</Model> 
     <DealerPriceNoFormat>8700.00</DealerPriceNoFormat> 
     <OptionsFinanceMonthlyPayment>300.50</OptionsFinanceMonthlyPayment> 
    </Vehicle> 
    <Vehicle> 
     <Model>KA</Model> 
     <DealerPriceNoFormat>10000.50</DealerPriceNoFormat> 
     <OptionsFinanceMonthlyPayment>270.50</OptionsFinanceMonthlyPayment> 
    </Vehicle> 
    <Vehicle> 
     <Model>Focus</Model> 
     <DealerPriceNoFormat>12000.00</DealerPriceNoFormat> 
     <OptionsFinanceMonthlyPayment>340.00</OptionsFinanceMonthlyPayment> 
    </Vehicle> 
    <Vehicle> 
     <Model>KA</Model> 
     <DealerPriceNoFormat>9910.00</DealerPriceNoFormat> 
     <OptionsFinanceMonthlyPayment>430.75</OptionsFinanceMonthlyPayment> 
    </Vehicle> 
    <Vehicle> 
     <Model>KUGA</Model> 
     <DealerPriceNoFormat>23010.00</DealerPriceNoFormat> 
     <OptionsFinanceMonthlyPayment>550.20</OptionsFinanceMonthlyPayment> 
    </Vehicle> 
    <Vehicle> 
     <Model>Focus</Model> 
     <DealerPriceNoFormat>15900.00</DealerPriceNoFormat> 
     <OptionsFinanceMonthlyPayment>430.00</OptionsFinanceMonthlyPayment> 
    </Vehicle> 
</Dealer> 

私が言ったように、そこに他のデータのロードがあるが、それは基本的な構造です:

私のXMLは少しのようになります。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > 
<xsl:output method="html" omit-xml-declaration="yes" indent="yes" version="4.0" encoding="iso-8859-1" /> 
    <xsl:key name="by-id" match="Dealer/Vehicle" use="Model"/> 
    <xsl:template match="Dealer"> 
    <xsl:copy> 
     <xsl:for-each select="Vehicle[generate-id() = generate-id(key('by-id', Model)[1])]"> 
     <xsl:for-each select="key('by-id', Model)"> 
      <xsl:sort select="DealerPriceNoFormat" data-type="number" order="ascending" /> 
      <xsl:if test="position()=1"> 
      <p> 
       <xsl:value-of select="Model" /><br /> 
       <xsl:value-of select="DealerPriceNoFormat" /><br /> 
       <xsl:value-of select="OptionsFinanceMonthlyPayment" /> 
      </p> 
      </xsl:if> 
     </xsl:for-each> 
     </xsl:for-each> 
    </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

私が言うように、私はちょうどそのOptionsFinanceMonthlyPaymentにより、出力リストをソートする方法を見つけ出すことはできませんが、ほとんどがよ:

そして、私のXSLTは、このようになります。出力以上の場合はそう

各モデルで最も安い車を示す、次のようになりますが、出力リストの毎月の支払いによって並べ替えられます:事前に

KA 
8700.00 
300.50 

Focus 
12000.00 
340.00 

KUGA 
23010.00 
550.20 

感謝。

+1

この場合、予想される出力を表示できますか?ありがとうございました! –

+0

必要な出力をデモするために投稿を編集しました。乾杯。 –

+0

"*出力リストの月額支払いでソート*"各グループの最も安い車の月額支払いでソートされていますか? –

答えて

2

私は2回のパスでこれを行うだろう - のようなもの:これはnode-set()拡張機能をサポートするプロセッサが必要であることを

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:exsl="http://exslt.org/common" 
extension-element-prefixes="exsl"> 

<xsl:key name="vehicle-by-model" match="Vehicle" use="Model"/> 

<xsl:template match="/Dealer"> 
    <!-- first-pass --> 
    <xsl:variable name="groups"> 
     <xsl:for-each select="Vehicle[generate-id() = generate-id(key('vehicle-by-model', Model)[1])]"> 
      <group> 
       <xsl:for-each select="key('vehicle-by-model', Model)"> 
        <xsl:sort select="DealerPriceNoFormat" data-type="number" order="ascending" /> 
        <xsl:if test="position()=1"> 
         <model><xsl:value-of select="Model" /></model> 
         <price><xsl:value-of select="DealerPriceNoFormat" /></price> 
         <pmt><xsl:value-of select="OptionsFinanceMonthlyPayment" /></pmt> 
        </xsl:if> 
       </xsl:for-each> 
      </group> 
     </xsl:for-each> 
    </xsl:variable> 
    <!-- output --> 
    <html> 
     <xsl:for-each select="exsl:node-set($groups)/group"> 
      <xsl:sort select="pmt" data-type="number" order="ascending" /> 
      <p> 
       <xsl:value-of select="model" /><br /> 
       <xsl:value-of select="price" /><br /> 
       <xsl:value-of select="pmt" /> 
      </p> 
     </xsl:for-each> 
    </html> 
</xsl:template> 

</xsl:stylesheet> 

注意を。

+1

夢のように動作します。私はそれをmsxsl:node-setに変更しなければなりませんでしたが、それは素晴らしいものです。助けてくれてありがとう。 –

関連する問題