如何编写将递归包含其他文件的 XSLT? [英] How can I write an XSLT that will recursively include other files?
本文介绍了如何编写将递归包含其他文件的 XSLT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一系列这种格式的 xml 文件:
Let's say I have a series of xml files in this format:
<page>
<header>Page A</header>
<content>blAh blAh blAh</content>
</page>
B.xml:
<page also-include="A.xml">
<header>Page B</header>
<content>Blah Blah Blah</content>
</page>
<小时>
使用此 XSLT:
Using this XSLT:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/page">
<h1>
<xsl:value-of select="header" />
</h1>
<p>
<xsl:value-of select="content" />
</p>
</xsl:template>
</xsl:stylesheet>
我可以把A.xml
变成这样:
<h1>
Page A
</h1>
<p>
blAh blAh blAh
</p>
但是我如何让它也将 B.xml
变成这个?
But how would I make it also turn B.xml
into this?
<h1>
Page B
</h1>
<p>
Blah Blah Blah
</p>
<p>
blAh blAh blAh
</p>
我知道我需要在某处使用 document(concat(@also-include,'.xml'))
,但我不确定在哪里.
I know that I need to use document(concat(@also-include,'.xml'))
somewhere, but I'm not sure where.
哦,问题是,如果 B 包含在第三个文件 C.xml
中,我需要它仍然工作.
Oh, and the catch is, I need this to still work if B were to be included in a third file, C.xml
.
知道如何做到这一点吗?
Any idea as to how to do this?
推荐答案
有可能:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="page">
<h1>
<xsl:value-of select="header"/>
</h1>
<p>
<xsl:apply-templates select="." mode="content"/>
</p>
</xsl:template>
<xsl:template match="page" mode="content">
<xsl:value-of select="content"/>
<xsl:if test="@include">
<xsl:apply-templates select="document(@include)" mode="content"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
这篇关于如何编写将递归包含其他文件的 XSLT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文