带有条件测试的xsl apply-templates

时间:2013-07-02 01:54:28

标签: xslt xslt-1.0

我一直在尝试将测试添加到我的xsl:apply templates元素中,但是我一直收到一条错误,上面写着“表达式没有评估到节点集”。我想知道是否有人可以指出我做错了什么来指出我正确的方向。

这是我的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

2 个答案:

答案 0 :(得分: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>

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

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

现在,逻辑。

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

<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视为只写语言,这可能不会造成任何特别的伤害,但在输出中这是一种不必要的丑陋。将您的条件放在正确的位置。

答案 1 :(得分: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>
相关问题