首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么xsl:对于每组多乘每一组内容多次

为什么xsl:对于每组多乘每一组内容多次
EN

Stack Overflow用户
提问于 2022-10-27 12:07:11
回答 1查看 33关注 0票数 0

lat说这是我的xml,xml应该解析为html。在有"eop“标签的地方,它是一个新页面。

我试着使用xsl:for-每个组,将xml除以"eop“,每个组的内容使用4次,任何人都能猜出为什么内容不像预期的那样出现一次吗?

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
    <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>

结果是

代码语言:javascript
复制
        <div> 
                       before eo                                 
                            after
                        before eo                                
                            after                                            
                         before eo    
                            after
                         before eo
                             </div>

预期结果(第一组):

<div> before eo</div>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-27 12:25:27

使用<xsl:value-of select="$groups?1?*[self::text()]"/>将为您提供直接包含在第一个组中的纯文本节点(而不是那些也是在组中收集的元素节点(如article1contentp)的后代)。

至于为每个“组”构造一个新的子树,这里有一种方法:

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74221767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档