gpt4 book ai didi

xslt - 围绕 xsl :apply-templates 的条件测试

转载 作者:行者123 更新时间:2023-12-01 06:20:24 27 4
gpt4 key购买 nike

我一直在尝试学习如何在 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 时,都会删除年份。有没有更好的方法可以使用应用模板?

最佳答案

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

<xsl:apply-templates select="year[not(../country='USA' and ../year='1985)]" />

关于xslt - 围绕 xsl :apply-templates 的条件测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11023458/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com