2017-01-05 14 views
0

XSLTの基本を学びたいと思っていますが、特定のユースケースに固執しています。私が達成したいのは、あるXMLファイルを別のXMLに変換することです(私はXSLT 2.0を使用しています)が、出力xmlの要素のグループ化は入力xmlの特定の要素の値によって決定されるという条件があります。入力xmlの要素の値に基づいてxmlを出力に変換

私は作成された例を通して私の質問を例示しようとします。

これは、入力XMLであると言うことができます:つまり、他の製品が持つ、私は、各製品のために、同様の製品を一覧表示するカテゴリ要素の値に基づいて

<products> 
<shoes> 
    <shoe> 
     <name>Ecco City</name> 
     <category>Urban</category> 
    </shoe> 
    <shoe> 
     <name>Timberland Forest</name> 
     <category>Wildlife</category> 
    </shoe> 
    <shoe> 
     <name>Asics Gel-Kayano</name> 
     <category>Running</category> 
    </shoe> 
</shoes> 
<clothes> 
    <shorts> 
     <name>North Face</name> 
     <category>Wildlife</category> 
    </shorts> 
    <shorts> 
     <name>Adidas Running Shorts</name> 
     <category>Running</category> 
    </shorts> 
</clothes> 

次のような入力XMLの同じカテゴリ:

<output> 
    <forSale> 
     <item>Asics Gel-Kayano</item> 
     <similarItem>Adidas Running Shorts</similarItem> 
    </forSale>  
</output> 
+0

研究XSLTのいずれかのグループ化の例2.0のようにhttps://www.w3.org/TR/xslt20/#grouping-examplesを入力してから、試してみて、どこにいるのか教えてください。 –

+0

あなたが表示する出力スニペットはあいまいです - 与えられたサンプル入力から得られると思われる完全な出力を投稿してください。 –

答えて

0

このようにグループ化の問題はないようです。私が正しく理解していれば、あなたのような何かをしたい:

ご入力の例に適用

XSLT 2.0

<xsl:stylesheet version="2.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="product-by-category" match="*" use="category" /> 

<xsl:template match="/products"> 
    <output> 
     <xsl:for-each select="*/*"> 
      <forSale> 
       <item> 
        <xsl:value-of select="name" /> 
       </item> 
       <xsl:for-each select="key('product-by-category', category) except ."> 
        <similarItem> 
         <xsl:value-of select="name" /> 
        </similarItem> 
       </xsl:for-each> 
      </forSale>  
     </xsl:for-each> 
    </output> 
</xsl:template> 

</xsl:stylesheet> 

を、結果は以下のようになります。

<?xml version="1.0" encoding="UTF-8"?> 
<output> 
    <forSale> 
     <item>Ecco City</item> 
    </forSale> 
    <forSale> 
     <item>Timberland Forest</item> 
     <similarItem>North Face</similarItem> 
    </forSale> 
    <forSale> 
     <item>Asics Gel-Kayano</item> 
     <similarItem>Adidas Running Shorts</similarItem> 
    </forSale> 
    <forSale> 
     <item>North Face</item> 
     <similarItem>Timberland Forest</similarItem> 
    </forSale> 
    <forSale> 
     <item>Adidas Running Shorts</item> 
     <similarItem>Asics Gel-Kayano</similarItem> 
    </forSale> 
</output> 
関連する問題