这就是我的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
任何帮助都将如此巨大!谢谢一群!
最佳答案
<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/>
注意事项:
完全不需要使用
xsl:apply-templates
。只有一个模板。
关于html - XSL重复相同的XML元素/节点,无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12996650/