这就是我的XML的样子。它非常简单,只是希望为每个链接布置一些链接到其他XML文件的链接:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="index.xsl"?>
<playToc>
<play>a_and_c.xml</play>
<play>all_well.xml</play>
<play>as_you.xml</play>
<play>com_err.xml</play>
<play>coriolan.xml</play>
<play>cymbelin.xml</play>
<name>Title 1</name>
<name>Title 2</name>
<name>Title 3</name>
<name>Title 4</name>
<name>Title 5</name>
<name>Title 6</name>
</playToc>
非常简单,对吧?所以这是我的XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="playToc">
<html>
<body style="text-align:center;">
<xsl:apply-templates select="play"></xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="play">
<xsl:variable name="URL">
<xsl:value-of select="."/>
</xsl:variable>
<xsl:variable name="TITLE">
<xsl:value-of select="../name"/>
</xsl:variable>
<a href="{$URL}"><xsl:value-of select="$TITLE"/></a>
<br />
</xsl:template>
</xsl:stylesheet>
这是输出:
Title 1
Title 1
Title 1
Title 1
Title 1
Title 1
当我希望这是输出时,当然:
Title 1
Title 2
Title 3
Title 4
Title 5
Title 6
任何帮助都会非常棒!非常感谢!
答案 0 :(得分:2)
XML输入的结构很差,但你可以通过
来解决这个问题<xsl:template match="play">
<xsl:variable name="pos" select="position()"/>
<a href="{.}">
<xsl:value-of select="../name[position() = $pos]"/>
</a>
<br/>
</xsl:template>
确保将<xsl:apply-templates select="play"/>
保留在其他模板中,否则使用position()
的方法将无效。
答案 1 :(得分:1)
<xsl:variable name="TITLE"> <xsl:value-of select="../name"/> </xsl:variable>
你的问题就在这里。
字符串值../name
是初始上下文(当前)节点的父节点的第一个 name
子节点的字符串值。
您真正想要的是获取与当前(name
)节点的位置具有相同位置的play
子项的值。
这种完整而短暂的转型:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="play">
<xsl:variable name="vPos" select="position()"/>
<a href="{.}">
<xsl:value-of select="../name[$vPos]"/>
</a><br />
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
应用于提供的源XML文档:
<playToc>
<play>a_and_c.xml</play>
<play>all_well.xml</play>
<play>as_you.xml</play>
<play>com_err.xml</play>
<play>coriolan.xml</play>
<play>cymbelin.xml</play>
<name>Title 1</name>
<name>Title 2</name>
<name>Title 3</name>
<name>Title 4</name>
<name>Title 5</name>
<name>Title 6</name>
</playToc>
生成想要的正确结果:
<a href="a_and_c.xml">Title 1</a>
<br/>
<a href="all_well.xml">Title 2</a>
<br/>
<a href="as_you.xml">Title 3</a>
<br/>
<a href="com_err.xml">Title 4</a>
<br/>
<a href="coriolan.xml">Title 5</a>
<br/>
<a href="cymbelin.xml">Title 6</a>
<br/>
请注意:
no 根本不需要使用xsl:apply-templates
。
只有一个模板。