我的输入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(Value节点是该关键节点的兄弟节点)。被困在这一段时间,试图应用多种条件。尝试以下方法: 1.尝试复制符合条件的节点 2.做身份证复印件&amp;忽略那些与条件不匹配的节点
不确定这是否可行,或者我做错了什么。这是我的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>
也尝试了其他组合。真的很感激任何帮助。
谢谢, ANI
答案 0 :(得分: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>
祝你好运, 彼得
答案 1 :(得分: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时,最近出现了很好的Gary Moore输出
<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-each来匹配 cd 元素:
<xsl:for-each select="cd[attributes/attribute[key=$KeyToBeMatched][value=$ValueToBeMatched]]">
以下是此方法的完整XSLT
<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>
这也将回归真棒加里摩尔。
答案 2 :(得分:0)
上有拼写错误
--------------------------------------------V---------------------
<xsl:for-each select="attributes/attribute[keu = $KeyToBeMatched]">