以下是以XML格式标记的文本示例。它包含细分<id>
,<ad>
和<od>
。我想在每个document/p
,<sum>incremented_letter</sum>
和<id>
之后遍历每个<ad>
以追加<od>
。这些字母从&#39; a&#39;开始。每个<document>
。当我使用xsl:number
成功创建细分并递增字母时,新细分将附加在p
的末尾。我没有掌握复制&#39;的XSLT语法。并且&#39;追加&#39;。
示例XML:
<root>
<document n="1">
<p>
<title>title here</title>
<seg>Lorem ipsum <id>foo</id> dolor sit amet, consectetur adipiscing elit.
Quisque a orci <ad>foo</ad> non mauris convallis imperdiet consequat nec
purus. Cras mollis lacus vel lectus facilisis, non
hendrerit velit tempor. Phasellus <id>foo</id> tempor urna vel accumsan
dignissim.</seg>
<seg>Aliquam porta eu nunc sed laoreet.
Sed non <od>foo</od> nulla consectetur, lobortis tortor ac,
placerat <ad>foo</ad> nunc. Nulla viverra finibus est nec efficitur. Donec
nisi nisl, <id>foo</id> tincidunt dapibus purus pretium, rhoncus volutpat
tortor. Cras fringilla tellus tortor, at
ultricies mi cursus at. Nulla facilisi.</seg>
<seg>Quisque id mi nisl. In in mauris id leo malesuada hendrerit.
Orci varius natoque
penatibus et magnis dis <od>foo</od> parturient montes, <ad>foo</ad> nascetur ridiculus
mus. Nulla sit amet
commodo mauris.</seg>
</p>
</document>
<document n="2">
<p>
<title>title here</title>
<seg>Lorem ipsum dolor <ad>foo</ad> sit <od>foo</od> amet, consectetur adipiscing elit.
Quisque a orci non mauris <id>foo</id> convallis imperdiet consequat nec
purus. Cras mollis lacus vel lectus facilisis, non
hendrerit velit tempor. Phasellus tempor urna vel accumsan
dignissim.</seg>
<seg>Aliquam porta eu nunc sed laoreet.
Sed non nulla <ad>foo</ad> consectetur, lobortis tortor ac,
placerat nunc. Nulla <id>foo</id> viverra finibus est nec efficitur. Donec
nisi <od>foo</od> nisl, tincidunt dapibus purus pretium, rhoncus volutpat
tortor. Cras fringilla tellus tortor, at
ultricies mi cursus at. Nulla facilisi.</seg>
<seg>Quisque id mi nisl. In in mauris id leo malesuada hendrerit.
Orci varius natoque
penatibus <id>foo</id> et magnis dis parturient montes, nascetur ridiculus
mus. Nulla sit amet
commodo mauris.</seg>
</p>
</document>
XSL 3.0:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="yes" />
<xsl:template match="p">
<xsl:apply-templates/>
<xsl:variable name="idadod" select=".//id | .//ad | .//od"/>
<xsl:for-each select="$idadod">
<sum><xsl:number count="$idadod" level="any" format="a"/></sum>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
目前不正确的结果:
<root>
<document n="1">
<title>title here</title>
<seg>Lorem ipsum <id>foo</id> dolor sit amet, consectetur adipiscing elit.
Quisque a orci <ad>foo</ad> non mauris convallis imperdiet consequat nec
purus. Cras mollis lacus vel lectus facilisis, non
hendrerit velit tempor. Phasellus <id>foo</id> tempor urna vel accumsan
dignissim.</seg>
<seg>Aliquam porta eu nunc sed laoreet.
Sed non <od>foo</od> nulla consectetur, lobortis tortor ac,
placerat <ad>foo</ad> nunc. Nulla viverra finibus est nec efficitur. Donec
nisi nisl, <id>foo</id> tincidunt dapibus purus pretium, rhoncus volutpat
tortor. Cras fringilla tellus tortor, at
ultricies mi cursus at. Nulla facilisi.</seg>
<seg>Quisque id mi nisl. In in mauris id leo malesuada hendrerit.
Orci varius natoque
penatibus et magnis dis <od>foo</od> parturient montes, <ad>foo</ad> nascetur ridiculus
mus. Nulla sit amet
commodo mauris.</seg>
<sum>a</sum>
<sum>b</sum>
<sum>c</sum>
<sum>d</sum>
<sum>e</sum>
<sum>f</sum>
<sum>g</sum>
<sum>h</sum>
</document>
<document n="2">
<title>title here</title>
<seg>Lorem ipsum dolor <ad>foo</ad> sit <od>foo</od> amet, consectetur adipiscing elit.
Quisque a orci non mauris <id>foo</id> convallis imperdiet consequat nec
purus. Cras mollis lacus vel lectus facilisis, non
hendrerit velit tempor. Phasellus tempor urna vel accumsan
dignissim.</seg>
<seg>Aliquam porta eu nunc sed laoreet.
Sed non nulla <ad>foo</ad> consectetur, lobortis tortor ac,
placerat nunc. Nulla <id>foo</id> viverra finibus est nec efficitur. Donec
nisi <od>foo</od> nisl, tincidunt dapibus purus pretium, rhoncus volutpat
tortor. Cras <ad>foo</ad> fringilla tellus tortor, at
ultricies mi cursus at. Nulla facilisi.</seg>
<seg>Quisque id mi nisl. In in mauris id leo malesuada hendrerit.
Orci varius natoque
penatibus <id>foo</id> et magnis dis parturient montes, nascetur ridiculus
mus. Nulla sit amet
commodo mauris.</seg>
<sum>a</sum>
<sum>b</sum>
<sum>c</sum>
<sum>d</sum>
<sum>e</sum>
<sum>f</sum>
<sum>g</sum>
</document>
期望的结果(样本):
<root>
<document n="1">
<title>title here</title>
<seg>Lorem ipsum <id>foo</id><sum>a</sum> dolor sit amet, consectetur adipiscing elit.
Quisque a orci <ad>foo</ad><sum>b</sum> non mauris convallis imperdiet consequat nec
purus. Cras mollis lacus vel lectus facilisis, non
hendrerit velit tempor. Phasellus <id>foo</id><sum>c</sum> tempor urna vel accumsan
dignissim.</seg>
<seg>Aliquam porta eu nunc sed laoreet.
Sed non <od>foo</od><sum>d</sum> nulla consectetur, lobortis tortor ac,
placerat <ad>foo</ad><sum>e</sum> nunc. Nulla viverra finibus est nec efficitur. Donec
nisi nisl, <id>foo</id><sum>f</sum> tincidunt dapibus purus pretium, rhoncus volutpat
tortor. Cras fringilla tellus tortor, at
ultricies mi cursus at. Nulla facilisi.</seg>
<seg>Quisque id mi nisl. In in mauris id leo malesuada hendrerit.
Orci varius natoque
penatibus et magnis dis <od>foo</od><sum>g</sum> parturient montes, <ad>foo</ad><sum>h</sum> nascetur ridiculus
mus. Nulla sit amet
commodo mauris.</seg>
</document>
.....
</root>
一切都在XSLTfiddle here非常感谢。
答案 0 :(得分:1)
如果您希望在每个<sum>
,<id>
和<ad>
之后立即生成<od>
,请将其移到与其匹配的模板内。复制其内容,然后生成<sum>
在处理完每个<p>
元素后,您已经生成了所有这些元素。
相反,添加模板以匹配<p>
并简单地处理它的子项,并在匹配这些<id>
,<ad>
和<od>
元素的模板中,复制他们的内容然后产生<sum>
元素。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="yes" />
<xsl:template match="p">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="id | ad | od">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<sum><xsl:number count="id | ad | od" from="p" format="a"/></sum>
</xsl:template>
</xsl:stylesheet>