题
我试图遍历的Docbook节节点。其结构如下:
<sect1>
<sect2>
<sect3>
<sect4>
<sect5>
</sect5>
</sect4>
</sect3>
</sect2>
</sect1>
所以仅SECT1已经sect2的内部,sect2的只会有sect3内,依此类推。我们也可以有一个节点内的多个子节点;用于SECT1内实例的多个sect2的。
编程方式我将递归地使用计数器用于跟踪哪个部分的环是在遍历它们。
这个时候我已经通过它使用XSLT和循环。因此有一个等同的方式,或在XSLT这样做的更好的方法?
编辑:我已经有通过威利,其中I指定每节节点(SECT1到sect5)建议类似的代码。我要寻找解决方案,它循环自行确定该教派节点,我不会有重复的代码。我知道的Docbook规格只允许最多5个嵌套节点。
解决方案
如果你正在做相同的处理对所有节{X}的节点,的{X},因为你在评论中的一个说,则以下是足够regardles 强>:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match=
"sect1|sect2|sect3|sect4|sect5">
<!-- Some processing here -->
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
如果你真的需要以相同的方式来处理许多具有的形式是“节”的不同的名称以上的元素{X}(假设x是在范围[1,100]),则可以使用如下:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match=
"*[starts-with(name(), 'sect')
and
substring-after(name(), 'sect') >= 1
and
not(substring-after(name(), 'sect') > 101)
]">
<!-- Some processing here -->
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
其他提示
<xsl:template match="sect1">
<!-- Do stuff -->
<xsl:apply-templates />
</xsl:template>
<xsl:template match="sect2">
<!-- Do stuff -->
<xsl:apply-templates />
</xsl:template>
<xsl:template match="sect3">
<!-- Do stuff -->
<xsl:apply-templates />
</xsl:template>
<xsl:template match="sect4">
<!-- Do stuff -->
<xsl:apply-templates />
</xsl:template>
<xsl:template match="sect5">
<!-- Do stuff -->
<xsl:apply-templates />
</xsl:template>
不隶属于 StackOverflow