lat说这是我的xml,xml应该解析为html。在有"eop“标签的地方,它是一个新页面。
我试着使用xsl:for-每个组,将xml除以"eop“,每个组的内容使用4次,任何人都能猜出为什么内容不像预期的那样出现一次吗?
<?xml version="1.0" encoding="utf-8"?>
<documentCollection>
<components>
<component>
<doc>
<mainBody>
<article_1>
<content>
<p>before eo
<eop eId="eop_386" />
after
</p>
</content>`
</article_1>
<article_2>
<content>
<p>point content</p>
</content>
</article_2>
<article_3>
<content>
<p>point content</p>
</content>
</article_3>
<article_4>
<content>
<p>before eo 387
<eop eId="eop_387" />
after 387</p>
</content>
</article_4>
<article_5>
<content>
<p> content 5</p>
</content>
</article_5>
<article_6>
<content>
<p> before eop 388
<eop eId="eop_388" />
after 388</p>
</content>
</article_6>
</mainBody>
</doc>
</component>
</components>
</documentCollection> 这是xslt:
<xsl:template match="doc">
<xsl:variable name="groups" as="array(*)*">
<xsl:for-each-group select="mainBody/descendant::node()" group-ending-with="eop">
<xsl:sequence select="array { current-group() }"/>
</xsl:for-each-group>
</xsl:variable>
<div class="explan_div">
<xsl:variable name="groups" select="fold-left($groups, [], function($a, $a1) { array:append($a, $a1) })"/>
<xsl:value-of select="$groups(1)"/>
</div>
</xsl:template>结果是
<div>
before eo
after
before eo
after
before eo
after
before eo
</div>预期结果(第一组):
<div> before eo</div>
发布于 2022-10-27 12:25:27
使用<xsl:value-of select="$groups?1?*[self::text()]"/>将为您提供直接包含在第一个组中的纯文本节点(而不是那些也是在组中收集的元素节点(如article1、content和p)的后代)。
至于为每个“组”构造一个新的子树,这里有一种方法:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
expand-text="yes"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
version="3.0">
<xsl:function name="mf:build-subtree" as="node()*">
<xsl:param name="group" as="node()*"/>
<xsl:apply-templates select="outermost($group)" mode="subtree">
<xsl:with-param name="group" select="$group" tunnel="yes"/>
</xsl:apply-templates>
</xsl:function>
<xsl:mode name="subtree" on-no-match="shallow-copy"/>
<xsl:template mode="subtree" match="node()">
<xsl:param name="group" tunnel="yes"/>
<xsl:if test="$group intersect .">
<xsl:next-match/>
</xsl:if>
</xsl:template>
<xsl:mode on-no-match="shallow-skip"/>
<xsl:output method="html" indent="yes" html-version="5"/>
<xsl:template match="doc">
<xsl:variable name="groups" as="array(*)*">
<xsl:for-each-group select="mainBody/descendant::node()" group-ending-with="eop">
<xsl:sequence select="array { current-group() => mf:build-subtree() }"/>
</xsl:for-each-group>
</xsl:variable>
<div class="explan_div">
<xsl:variable name="groups" select="fold-left($groups, [], function($a, $a1) { array:append($a, $a1) })"/>
<xsl:value-of select="$groups?1"/>
</div>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/74221767
复制相似问题