5

我一直在尝试学习如何在 xslt 中编写代码,但目前我一直坚持如何在 xsl:apply-templates 标记周围使用条件测试。

这是我正在测试的 xml。

<?xml version="1.0" encoding="utf-8"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
</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>
</cd>
<cd>
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <country>USA</country>
    <company>RCA</company>
    <price>9.90</price>
    <year>1982</year>
</cd>

这是我的 xslt

<xsl:template match="/">
  <xsl:apply-templates select="catalog/cd" />
</xsl:template>

<xsl:template match="cd">
  <p>
    <xsl:apply-templates select="artist" />
    <br /> 
    <xsl:apply-templates select="country" />
    <br />
    <xsl:if test="country != 'USA' and year != '1985'">
      <xsl:apply-templates select="year" />
    </xsl:if>
  </p>
</xsl:template>

<xsl:template match="artist">
  <xsl:value-of select="." />
</xsl:template>

<xsl:template match="country">
  <xsl:value-of select="." />
</xsl:template>

<xsl:template match="year">
  <xsl:value-of select="." />
</xsl:template>

这是我的输出:

Bob Dylan
USA

Bonnie Tyler
UK
1988

Dolly Parton
USA

这是我期待的输出:

Bob Dylan
USA

Bonnie Tyler
UK
1988

Dolly Parton
USA
1982

即使我只想在国家/地区的值为 USA 并且年份的值为 1985 时才删除年份,但每次国家/地区的值为 USA 时都会删除年份。有没有更好的方法可以使用应用模板?

4

4 回答 4

5

您可能更喜欢直接将模板应用于所需的节点集,而不需要条件“if”检查。

<xsl:apply-templates select="year[not(../country='USA' and ../year='1985)]" />
于 2012-06-13T21:34:41.017 回答
1

简单地纠正你的逻辑

替换

<xsl:if test="country != 'USA' and year != '1985'">

<xsl:if test="country != 'USA' or year != '1985'">

甚至更好的是,仅在想要的节点上应用模板

  <xsl:apply-templates select=
  "self::*[country != 'USA' or year != '1985']/year"/>

请注意

如图所示,可以在select属性中指定 Xpath 表达式,而无需使用任何反向轴(来回移动)。

于 2012-06-14T02:32:22.523 回答
1

这对我有帮助,谢谢。我正在扩展这个示例并尝试为某个属性调用一个模板(我已将 XML 编辑为例如<artist sex="male">Bob Dylan</artist> 我的尝试失败但我解决了。这段代码允许我在 >1 场景中创建条件模板:-

<xsl:template match="cd">
    <p>
        <xsl:apply-templates select="title"/>  
        <xsl:apply-templates select="artist [@sex = 'male']"/>
        <xsl:apply-templates select="artist [@sex = 'female']"/>
        <xsl:apply-templates select="year"/>
    </p>
</xsl:template>

<xsl:template match="title">
    Title: <span style="color:#ff0000">
        <xsl:value-of select="."/></span>
    <br />
</xsl:template>

<xsl:template match="artist [@sex = 'male']">
    Artist: <span style="color:#e9d419">
        <xsl:value-of select="."/></span>
    <br />
</xsl:template>

<xsl:template match="artist [@sex = 'female']">
    Artist: <span style="color:#48b8ff">
        <xsl:value-of select="."/></span>
    <br />
</xsl:template>

<xsl:template match="year">
    Year: <span style="color:#eb47dc">
        <xsl:value-of select="."/></span>
    <br />
</xsl:template>

希望这对某人有所帮助和有用!输出会根据性别产生不同颜色的艺术家姓名。

于 2014-04-30T10:32:32.980 回答
-1

如果国家是美国并且年份是 1985,您想删除年份。

因此,如果国家/地区不是美国年份不是 1985 年,则要复制年份。

您的条件应该使用 anor而不是and. 请注意not(a and b) = (not a) or (not b).

于 2012-06-13T21:25:16.353 回答