2014-05-22 21 views
1

我需要幫助XSLT比較值...輸入XML,我有:XSLT從XML文檔的不同分支

<shop> 
    <categories> 
    <category categoryId="63" parentCategoryId="239"> 
     <name>Fruit</name> 
    </category> 
    <category categoryId="62" parentCategoryId="239"> 
     <name>Vegetable</name> 
    </category> 
    <category categoryId="60" parentCategoryId="221"> 
     <name>Furniture</name> 
    </category> 
    ... 
    <categories> 
    <products> 
    <product productId="3" productCode="1.05"> 
     <name>Chair</name> 
     <categories> 
     <category>60</category> 
     </categories> 
    </product> 
    <product productId="21" productCode="1.59"> 
     <name>Apple</name> 
     <categories> 
     <category>63</category> 
     </categories> 
    </product> 
    ... 
    </products> 
</shop> 

我需要創建的產品,在那裏我有產品的名稱和列表其類別的名稱。我怎樣才能做到這一點?這不工作對我來說...

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > 

    <xsl:template match="shop"> 
    <export> 
     <xsl:apply-templates select="products" /> 
    </export> 
    </xsl:template> 


    <xsl:template match="products"> 
    <xsl:for-each select="product"> 
     <polozka> 
     <product_name> 
      <xsl:value-of select="name" /> 
     </product_name> 
     <xsl:for-each select="/shop/categories/category"> 
      <xsl:if test="boolean(@categoryId = /shop/products/product/categories/category)"> 
      <category_name> 
       <xsl:value-of select="name" /> 
      </category_name> 
      </xsl:if> 
     </xsl:for-each> 
     </polozka>  
    </xsl:for-each> 
    </xsl:template> 

</xsl:stylesheet> 

我會很高興,如果有任何人誰可以幫我...

+0

你能添加的樣本看起來像?謝謝! –

回答

0

您比較每個類別的@CategoryID所有產品:

@categoryId = /shop/products/product/categories/category 

這將永遠是真實的,因爲每個產品都與現有類別相關。

您必須將其與當前產品的類別進行比較。你不需要<xsl:if>。它可以用謂詞來完成。用以下代碼替換您的代碼:

<xsl:for-each select="/shop/categories/category[@categoryId = current()/categories/category]"> 
    <category_name> 
     <xsl:value-of select="name" /> 
    </category_name> 
</xsl:for-each> 

現在應該過濾出與當前產品類別不匹配的類別。

1

如果你想查找一個基於其@CategoryID則可以考慮使用一個鍵做的仰視

然後,假設每個產品只屬於一個類別類別 ,您可以簡單地查找產品的類別名稱,例如

<xsl:value-of select="key('category', categories/category)/name"/> 

試試這個XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:key name="category" match="category" use="@categoryId"/> 

    <xsl:template match="shop"> 
     <export> 
     <xsl:apply-templates select="products"/> 
     </export> 
    </xsl:template> 

    <xsl:template match="products"> 
     <xsl:for-each select="product"> 
     <polozka> 
      <product_name> 
       <xsl:value-of select="name"/> 
      </product_name> 
      <category_name> 
       <xsl:value-of select="key('category', categories/category)/name"/> 
      </category_name> 
     </polozka> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

如果產品元素可以有多個字符,那麼你就需要使用的for-each循環,而不是你所期望的輸出也

<xsl:for-each select="categories/category"> 
    <category_name> 
     <xsl:value-of select="key('category', .)/name"/> 
    </category_name> 
</xsl:for-each>