2012-04-12 44 views
0

我的輸入XML具有以下結構:問題與具有處理XSLT多個條件

<catalog> 
<cd> 
    <title>Empire Burlesque</title> 
    <artist>Bob Dylan</artist> 
    <country>USA</country> 
    <company>Columbia</company> 
    <price>10.90</price> 
    <year>1985</year> 
    <attributes> 
     <attribute> 
      <key>1</key> 
      <value>one</value> 
     </attribute> 
     <attribute> 
      <key>2</key> 
      <value>two</value> 
     </attribute> 
    </attributes> 
</cd> 
<cd> 
    <title>Hide your heart</title> 
    <artist>Bonnie Tyler</artist> 
    <country>UK</country> 
    <company>CBS Records</company> 
    <price>9.90</price> 
    <year>1988</year> 
    <attributes> 
     <attribute> 
      <key>1</key> 
      <value>one</value> 
     </attribute> 
     <attribute> 
      <key>2</key> 
      <value>two</value> 
     </attribute> 
    </attributes> 
</cd> 
<cd> 
    <title>Greatest Hits</title> 
    <artist>Dolly Parton</artist> 
    <country>USA</country> 
    <company>RCA</company> 
    <price>9.90</price> 
    <year>1982</year> 
    <attributes> 
     <attribute> 
      <key>1</key> 
      <value>one</value> 
     </attribute> 
     <attribute> 
      <key>2</key> 
      <value>two</value> 
     </attribute> 
    </attributes> 
</cd> 
<cd> 
    <title>Still got the blues</title> 
    <artist>Gary Moore</artist> 
    <country>UK</country> 
    <company>Virgin records</company> 
    <price>10.20</price> 
    <year>1990</year> 
    <attributes> 
     <attribute> 
      <key>1</key> 
      <value>WON</value> 
     </attribute> 
     <attribute> 
      <key>2</key> 
      <value>two</value> 
     </attribute> 
    </attributes> 
</cd> 
</catalog> 

現在,我試圖通過只選擇那些CD節點創建不同的XML爲哪些{鍵= 1和值= WON}(該節點是該節點的兄弟節點)。一直堅持這一點,試圖應用多種條件。嘗試以下方法:1。 嘗試複製那些符合條件 2.做一個身份的節點複製&忽略不符合條件

不知道這是可行的,還是我做錯事的節點。這是我的xslt看起來像:

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

<xsl:variable name="KeyToBeMatched">1</xsl:variable> 
<xsl:param name="ValueToBeMatched">WON</xsl:param> 


<xsl:template match="catalog"> 

     <xsl:for-each select="cd"> 
     <xsl:for-each select="attributes/attribute[keu = $KeyToBeMatched]"> 
      <xsl:variable name="attributeValue" select="value"/> 
       <xsl:if test="$attributeValue = $RMGAccountId"> 
         <xsl:copy> 
          <xsl:apply-templates select="@*|*|text()" /> 
         </xsl:copy> 
       </xsl:if> 
     </xsl:for-each> 
     </xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 

也試過其他組合。非常感謝這方面的幫助。

感謝, 安仁

回答

1

我不知道,如果你想整個CD元素複製或它不應該有一個匹配的屬性,但如果你這樣做,你可以簡單地增加一個匹配模板像這樣,在其中再加入代碼複製元素

<xsl:template match="cd[attributes/attribute[key='1'][value='WON']]"> 
    <!-- Copy element --> 
</xsl:template> 

其他CD元素就可以匹配和忽略

<xsl:template match="cd" /> 

試試這個XSLT:

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

    <xsl:template match="cd[attributes/attribute[key='1'][value='WON']]"> 
     <xsl:call-template name="identity" /> 
    </xsl:template> 

    <xsl:template match="cd" /> 

    <xsl:template match="@*|node()" name="identity"> 
     <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

當適用於您的示例XML,出來的晚,偉大的加里·摩爾是輸出

<catalog> 
    <cd> 
     <title>Still got the blues</title> 
     <artist>Gary Moore</artist> 
     <country>UK</country> 
     <company>Virgin records</company> 
     <price>10.20</price> 
     <year>1990</year> 
     <attributes> 
     <attribute> 
      <key>1</key> 
      <value>WON</value> 
     </attribute> 
     <attribute> 
      <key>2</key> 
      <value>two</value> 
     </attribute> 
     </attributes> 
    </cd> 
</catalog> 

當然,在你的情況下,您需要參數化匹配值,在這種情況下,您不能使用此方法,因爲變量不能用於模板匹配。相反,你可以使用的xsl:for-每到元素匹配CD:

<xsl:for-each select="cd[attributes/attribute[key=$KeyToBeMatched][value=$ValueToBeMatched]]"> 

下面是這種方法

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

    <xsl:variable name="KeyToBeMatched">1</xsl:variable> 
    <xsl:param name="ValueToBeMatched">WON</xsl:param> 

    <xsl:template match="catalog"> 
     <xsl:for-each select="cd[attributes/attribute[key=$KeyToBeMatched][value=$ValueToBeMatched]]"> 
     <xsl:call-template name="identity"/> 
     </xsl:for-each> 
    </xsl:template> 
    <xsl:template match="@*|node()" name="identity"> 
     <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

這也將返回真棒加里·摩爾的全部XSLT。

+0

非常感謝Tim。你救了我的一天:) – 2012-04-12 08:34:34

+0

一個相關的問題:我在匹配表達式中使用的值需要動態傳遞。如果我把一個參數,而不是硬編碼的'WON',這是不被接受的。我能做到這一點嗎? – 2012-04-12 08:37:04

+0

啊,是的,你是對的。我已經擴展了我的答案,以顯示在這種情況下如何使用參數。 – 2012-04-12 08:45:23

0

有上

--------------------------------------------V--------------------- 
<xsl:for-each select="attributes/attribute[keu = $KeyToBeMatched]"> 
1

一個錯字試試這個:

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

<xsl:variable name="KeyToBeMatched">1</xsl:variable> 
<xsl:param name="ValueToBeMatched">WON</xsl:param> 

<xsl:template match="list"> 
    <xsl:for-each select="cd"> 
     <xsl:for-each select="attributes/attribute[key = $KeyToBeMatched]"> 
      <xsl:variable name="attributeValue" select="value"/> 
      <xsl:if test="$attributeValue = $ValueToBeMatched"> 
       <xsl:copy> 
        <xsl:apply-templates select="@*|*|text()" /> 
       </xsl:copy> 
      </xsl:if> 
     </xsl:for-each> 
    </xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 

變量 「RMGAccountId」 從來沒有宣佈,我交換,與 「ValueToBeMatched」 。並修復了Filype指出的錯字。應用於源你得到這個XML:

<?xml version="1.0" encoding="UTF-8"?> 
<attribute> 1 WON </attribute> 

最好的問候, 彼得