XSLT-hyperlink基于属性的XML元素
我有以下存储电影和演员的XML:XSLT-hyperlink基于属性的XML元素,xml,xslt,hyperlink,Xml,Xslt,Hyperlink,我有以下存储电影和演员的XML: <movies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="movies.xsd"> <movie movieID="1"> <cast> <actors> <actor actorID="1">
<movies
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="movies.xsd">
<movie movieID="1">
<cast>
<actors>
<actor actorID="1">
<name>Bob</name>
</actor>
<actor actorID="2">
<name>John</name>
</actor>
<actor>
<name>Mike</name>
</actor>
</actors>
</cast>
</movie>
</movies>
上下快速移动
约翰
迈克
前两个参与者有一个具有唯一值的属性“actorID”。第三个参与者没有属性。
我想显示前两个演员的名字作为超链接,并显示第三个演员
名称为纯文本
这是我的XSLT:
<xsl:template match="/">
<xsl:apply-templates select="movies/movie" />
</xsl:template>
<xsl:template match="movie">
<xsl:text>Actors: </xsl:text>
<xsl:apply-templates select="cast/actors/actor[@actorID]/name"/>
</xsl:template>
<xsl:template match="actor[@actorID]/name">
<xsl:element name="a">
<xsl:attribute name="href">www.mywebsite.com</xsl:attribute>
<xsl:value-of select="." />
</xsl:element>
<xsl:element name="br" />
</xsl:template>
<xsl:template match="actor/name">
<xsl:value-of select="." />
<xsl:element name="br" />
</xsl:template>
演员:
www.mywebsite.com
我得到的输出是Bob和John显示为纯文本,而Mike根本不显示。因此,情况恰恰相反
我想要实现的目标。您的XPath在这里:
<xsl:apply-templates select="cast/actors/actor[@actorID]/name"/>
那么XSLT的行为应该与您期望的一样
作为旁注,我建议在XSLT中使用文字元素,除非需要使用xsl:element
:
<xsl:template match="actor[@actorID]/name">
<a href="http://www.mywebsite.com">
<xsl:value-of select="." />
</a>
<br />
</xsl:template>
<xsl:template match="actor/name">
<xsl:value-of select="." />
<br />
</xsl:template>
它使XSLT更易于阅读。如果需要在属性中包含值,可以使用属性值模板:
<a href="http://www.mywebsite.com/actors?id={../@actorID}">
谢谢您的回复,JLRishe。我已经将您的解决方案应用到我的代码中,现在Bob、John和Mike显示为纯文本。第二个模板匹配似乎覆盖了第一个模板匹配。是否修改了此行:
?那一个应该保持不变,只需修改apply模板
。如果您只修改了应用模板
,而第二个模板仍然覆盖第一个模板,则可以尝试向第一个模板添加优先级属性:
优先级属性完成了任务。这显示了有多少种方法可以解决xslt中的问题。一如既往,非常感谢你的帮助,杰里什。
<a href="http://www.mywebsite.com/actors?id={../@actorID}">