我一直在尝试向我的 xsl:apply 模板元素添加测试,但我不断收到一条错误消息,提示“表达式不计算为节点集”。我想知道是否有人可以指出我做错了什么以指出我正确的方向。
这是我的 XML
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<band>
<guitar>Joe</guitar>
<drums>Rachel</drums>
<bass>Mike</bass>
</band>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<band>
<guitar>Cat</guitar>
<drums>Paul</drums>
<bass>Bobby</bass>
</band>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<band>
<guitar>Eric</guitar>
<drums>Bill</drums>
<bass>Jason</bass>
</band>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
</catalog>
这是我的 XSLT:
<?xml version="1.0" encoding="utf-8"?>
<!-- DWXMLSource="Catalog.xml" -->
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="catalog">
<xsl:apply-templates select="cd" />
</xsl:template>
<xsl:template match="cd">
<p style="color:red;">
<xsl:apply-templates select="title = 'Empire Burlesque'" />
</p>
<p style="color:blue;">
<xsl:apply-templates select="artist = 'Bob Dylan'" />
</p>
<p style="color:green;">
<xsl:apply-templates select="band/guitar = 'Joe'" />
</p>
</xsl:template>
<xsl:template match="title">
Title: <xsl:apply-templates />
</xsl:template>
<xsl:template match="artist">
Artist: <xsl:apply-templates />
</xsl:template>
<xsl:template match="band/guitar">
Guitar: <xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
我期待的结果是:
Title: Empire Burlesque
Artist: Bob Dylan
Guitar: Joe