我的输入 XML 具有以下结构:
<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>
现在,我试图通过仅选择 { key = 1 和 value = WON } 的那些 cd 节点(与该键节点同级的值节点)来创建不同的 xml。一直坚持这一点,试图应用多个条件。尝试了以下方法:
- 尝试复制那些匹配条件的节点
- 进行身份复制并忽略那些不符合条件的节点
不确定这是否可行,或者我做错了什么。这是我的 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>