好的,继续我的问题here https://stackoverflow.com/questions/2907332/how-can-i-write-an-xslt-that-will-recursively-include-other-files.
假设我的页面现在是这样的:
A.xml:
<page>
<header>Page A</header>
<content-a>Random content for page A</content-a>
<content-b>More of page A's content</content-b>
<content-c>More of page A's content</content-c>
<!-- This doesn't keep going: there are a predefined number of sections -->
</page>
B.xml:
<page include="A.xml">
<header>Page B</header>
<content-a>Random content for page B</content-a>
<content-b>More of page B's content</content-b>
<content-c>More of page B's content</content-c>
</page>
C.xml:
<page include="B.xml">
<header>Page C</header>
<content-a>Random content for page C</content-a>
<content-b>More of page C's content</content-b>
<content-c>More of page C's content</content-c>
</page>
变换后(上C.xml
),我想结束这样的事情:
<h1>Page C</h1>
<div>
<p>Random content for page C</p>
<p>Random content for page B</p>
<p>Random content for page A</p>
</div>
<div>
<p>More of page C's content</p>
<p>More of page B's content</p>
<p>More of page A's content</p>
</div>
<div>
<p>Yet more of page C's content</p>
<p>Yet more of page B's content</p>
<p>Yet more of page A's content</p>
</div>
我知道我可以使用document(@include)
包含另一个文档。然而,递归有点超出了我的范围。
我将如何编写这样的转换?
这是一个 XSLT 2.0 解决方案:
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:template match="page">
<xsl:variable name="pages">
<xsl:apply-templates select="." mode="load" />
</xsl:variable>
<xsl:copy>
<h1><xsl:value-of select="header" /></h1>
<!-- you say there is a fixed number of names, so this should be OK -->
<xsl:for-each select="'content-a','content-b','content-c'">
<div>
<xsl:apply-templates select="$pages/page/*[name() = current()]" />
</div>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="page" mode="load">
<xsl:sequence select="." />
<xsl:apply-templates select="document(@include)" mode="load" />
</xsl:template>
<xsl:template match="content-a|content-b|content-c">
<p><xsl:value-of select="." /></p>
</xsl:template>
</xsl:stylesheet>
编辑:对于 XSLT 1.0,等效的解决方案如下所示:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
>
<xsl:template match="page">
<xsl:variable name="pages-rtf"><!-- rtf = result tree fragment -->
<xsl:apply-templates select="." mode="load" />
</xsl:variable>
<xsl:variable name="pages" select="exsl:node-set($pages-rtf)" />
<!-- you say there is a fixed number of names, so this should be OK -->
<xsl:variable name="nodes-rtf">
<content-a/><content-b/><content-c/>
</xsl:variable>
<xsl:variable name="nodes" select="exsl:node-set($nodes-rtf)" />
<xsl:copy>
<h1><xsl:value-of select="header" /></h1>
<xsl:for-each select="$nodes">
<div>
<xsl:apply-templates select="$pages/page/*[name() = name(current())]" />
</div>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="page" mode="load">
<xsl:copy-of select="." />
<xsl:apply-templates select="document(@include)" mode="load" />
</xsl:template>
<xsl:template match="content-a|content-b|content-c">
<p><xsl:value-of select="." /></p>
</xsl:template>
</xsl:stylesheet>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)