【发布时间】:2013-09-25 15:38:20
【问题描述】:
我是 XSL 的新手,我正在尝试将具有以下结构的 XML 文件展平,目的是在 InDesign 中使用它(真正的 XML 结构要复杂得多,实际上遵循 NLM 架构,但下面的示例应该努力说明我需要什么):
例如
<section>
<p> just normal text here 1</p>
<section>
<p>just normal text for this section</p>
</section>
<p>just normal text here 2</p>
<section>
<p>just more text</p>
<section>
<p>this is the text in the deepest section </p>
</section>
<p>and even more text </p>
</section>
<p>just normal text here 3</p>
</section>
使用以下 XSLT,我几乎已经完成了我需要的工作:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="section">
<xsl:variable name="sectionlevel">
<xsl:value-of select="count(ancestor::section)" />
</xsl:variable>
<xsl:element name="s{$sectionlevel}">
<xsl:apply-templates select="descendant::section" />
<xsl:copy-of select="*[local-name() != 'section']"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我使用这段代码得到的输出如下所示,这很好,但问题是我需要保持元素的顺序。我只需要更改部分元素名称并保持其他所有内容相同:
<?xml version="1.0" encoding="utf-8"?>
<s0>
<s1>
<p> just normal text for this section</p>
</s1>
<s1>
<s2>
<p>this is the text in the deepest section </p>
</s2>
<p>just more text</p>
<p>and even more text </p>
</s1>
<s2>
<p>this is the text in the deepest section </p>
</s2>
<p> just normal text here 1</p>
<p>just normal text here 2</p>
<p>just normal text here 3</p>
</s0>
如你所见
此示例中的元素被移动到 section 元素内的末尾。我应该如何编码 XSL 转换,以便保留所有原始 XML 顺序和结构并只更改部分标签?
【问题讨论】:
标签: xslt adobe-indesign flatten