我的輸入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>
也試過其他組合。非常感謝這方面的幫助。
感謝, 安仁
非常感謝Tim。你救了我的一天:) – 2012-04-12 08:34:34
一個相關的問題:我在匹配表達式中使用的值需要動態傳遞。如果我把一個參數,而不是硬編碼的'WON',這是不被接受的。我能做到這一點嗎? – 2012-04-12 08:37:04
啊,是的,你是對的。我已經擴展了我的答案,以顯示在這種情況下如何使用參數。 – 2012-04-12 08:45:23