【发布时间】:2010-10-20 21:03:08
【问题描述】:
假设我有一个这样的 xml 文档:
<director>
<play>
<t>Nutcracker</t>
<a>Tom Cruise</a>
</play>
<play>
<t>Nutcracker</t>
<a>Robin Williams</a>
</play>
<play>
<t>Grinch Stole Christmas</t>
<a>Will Smith</a>
</play>
<play>
<t>Grinch Stole Christmas</t>
<a>Mel Gibson</a>
</play>
</director>
现在我希望能够选择 Will Smith 作为演员的所有戏剧并将其重新格式化为如下内容:
<Plays>
<Play title="Grinch Stole Christmas">
<star>Will Smith</star>
<star>Mel Gibson</star>
</Play>
</Plays>
我只想使用 apply-templates.. 没有 xsl:if 或 for each 循环(我设计了这个示例作为我正在做的更简单的版本,因此您可以帮助我了解如何在匹配中使用 xpath声明)
这是我目前所拥有的:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/director">
<Plays>
<xsl:apply-templates select="play"/>
</Plays>
</xsl:template>
<xsl:template match="play[a='Will Smith']">
<play title="{data(t)[1]}">
<xsl:apply-templates select="a"/>
</play>
</xsl:template>
<xsl:template match="a">
<star>
<xsl:value-of select="."/>
</star>
</xsl:template>
</xsl:stylesheet>
基本上我只是不确定如何在模板的 match 属性中使用 XPath 过滤掉节点。任何帮助都会很棒!
【问题讨论】: