0

我一直在尝试向我的 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   "&#160;">
<!ENTITY copy   "&#169;">
<!ENTITY reg    "&#174;">
<!ENTITY trade  "&#8482;">
<!ENTITY mdash  "&#8212;">
<!ENTITY ldquo  "&#8220;">
<!ENTITY rdquo  "&#8221;"> 
<!ENTITY pound  "&#163;">
<!ENTITY yen    "&#165;">
<!ENTITY euro   "&#8364;">
]>
<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
4

2 回答 2

1

考虑模板

<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>

这说明了语法问题和逻辑问题。

语法第一:给定一个 cd 元素作为当前节点,表达式“title”计算为一个节点集。表达式“title = 'Empire Burlesque'”的计算结果为布尔值。如果您想将模板应用于每个具有字符串值“Empire Burlesque”的标题子项,您需要编写类似“title[.='Empire Burlesque']”的内容。一旦你修复了所有三个选择表达式,你就会得到你期望的输出。

现在,逻辑。

将为输入中的每个 cd 元素评估一次此模板。因此,一旦您修复了选择表达式,您将获得预期的输出,然后是

<p style="color:red;"/>
<p style="color:blue;"/>
<p style="color:green;"/>
<p style="color:red;"/>
<p style="color:blue;"/>
<p style="color:green;"/>

前三个空段落将由 Bonnie Tyler CD 生成,后三个由 Dolly Parton 生成。

如果您将 HTML 视为一种只写语言,这可能不会造成任何特别的伤害,但它是您输出中不必要的丑陋。将您的条件放在正确的位置。

于 2013-07-02T02:08:44.890 回答
1

条件按以下方式制作:

<xsl:template match="cd">
  <p style="color:red;">
    <xsl:apply-templates select="title[text()='Empire Burlesque']" />
  </p>
  <p style="color:blue;">
    <xsl:apply-templates select="artist[text()='Bob Dylan']" />
  </p>
  <p style="color:green;">
    <xsl:apply-templates select="band/guitar[text()='Joe']" />
  </p>
</xsl:template>
于 2013-07-02T02:21:27.207 回答