在我的问题XSL change structure of ODT XML file 之后,我无法解决此线程中给出的解决方案如何在我的情况下将平面 xml 数据转换为分层数据 xml。
我稍微更改了输入 xml(标题为 h 而不是 p):
<body>
<h class="head1">1: Heading level 1</h>
<p>some text here</p>
<p>some text here</p>
<h class="head2">1.1: Heading level 2</h>
<p>some text here</p>
<p>some text here</p>
<h class="head3">1.1.1: Heading level 3</h>
<p>some text here</p>
<p>some text here</p>
<h class="head1">2: Heading level 1</h>
<h class="head2">2.1: Heading level 2</h>
<p>some text here</p>
<p>some text here</p>
<h class="head3">2.1.1: Heading level 3</h>
<p>some text here</p>
<p>some text here</p>
<h class="head3">2.1.2: Heading level 3</h>
<p>some text here</p>
<p>some text here</p>
</body>
而且我不知道如何适应以下 Xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mf="http://example.com/mf"
exclude-result-prefixes="xs mf" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:function name="mf:group" as="element(section)*">
<xsl:param name="entries" as="element(p)*"/>
<xsl:param name="level" as="xs:integer"/>
<xsl:for-each-group select="$entries"
group-starting-with="p[@class = concat('head',$level)]">
<xsl:variable name="P_ID" select="generate-id(.)"/>
<section name="{@class}">
<title>
<xsl:value-of select="."/>
</title>
<xsl:if test="following-sibling::p[1][not(@class)]">
<ps>
<xsl:apply-templates
select="following-sibling::p[not(@class)][generate-id(preceding-sibling::p[@class][1]) = $P_ID]"
/>
</ps>
</xsl:if>
<xsl:sequence select="mf:group(current-group() except ., ($level + 1))"/>
</section>
</xsl:for-each-group>
</xsl:function>
<xsl:template match="body">
<xsl:copy>
<xsl:sequence select="mf:group(p[contains(@class,'head')], 1)"/>
</xsl:copy>
</xsl:template>
为了得到这个结果
<body>
<section name="head1">
<title>1: Heading level 1</title>
<ps>
<p>some text here</p>
<p>some text here</p>
</ps>
<section name="head2">
<title>1.1: Heading level 2</title>
<ps>
<p>some text here</p>
<p>some text here</p>
</ps>
<section name="head3">
<title>1.1.1: Heading level 3</title>
<ps>
<p>some text here</p>
<p>some text here</p>
</ps>
</section>
</section>
</section>
<section name="head1">
<title>2: Heading level 1</title>
<section name="head2">
<title>2.1: Heading level 2</title>
<ps>
<p>some text here</p>
<p>some text here</p>
</ps>
<section name="head3">
<title>2.1.1: Heading level 3</title>
<ps>
<p>some text here</p>
<p>some text here</p>
</ps>
</section>
<section name="head3">
<title>2.1.2: Heading level 3</title>
<ps>
<p>some text here</p>
<p>some text here</p>
</ps>
</section>
</section>
</section>
</body>
对我在引用的上一个问题中的回答的改编和简化是
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:function name="mf:group" as="element()*">
<xsl:param name="elements" as="element()*"/>
<xsl:param name="level" as="xs:integer"/>
<xsl:for-each-group select="$elements" group-starting-with="h[@class = concat('head', $level)]">
<xsl:choose>
<xsl:when test="not(self::h[@class = concat('head', $level)])">
<xsl:where-populated>
<ps>
<xsl:apply-templates select="current-group()"/>
</ps>
</xsl:where-populated>
</xsl:when>
<xsl:otherwise>
<section name="{@class}">
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:sequence select="mf:group(current-group() except ., ($level + 1))"/>
</section>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:function>
<xsl:template match="body">
<xsl:copy>
<xsl:sequence select="mf:group(*, 1)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/jxDjimV
使用 XSLT 3xsl:mode
将身份转换声明为基本转换,但您可以将其拼写为 XSLT 2 处理器的模板。并且xsl:where-populated
它也是 XSLT 3,但xsl:when test"
如果需要,可以用更具体的测试替换。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句