首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要在<student>元素中添加<more></more>

需要在<student>元素中添加<more></more>
EN

Stack Overflow用户
提问于 2021-10-19 11:07:48
回答 1查看 32关注 0票数 0

如果我们在@more=元素中获得了“1”属性,那么<more>元素应该是add just <following-sibling> of <student>。如果添加“2”属性,则应在两个<following-sibling> <student>元素中添加<more>元素

输入XML:

代码语言:javascript
复制
<kk>
    <kita>
    <student>
        <roll>content here</roll>
        <roll>content here</roll>
        <roll>content here</roll>
        <roll more="1">content here</roll>
    </student>
    <student>
        <roll>content here</roll>
        <roll>content here</roll>
        <roll>content here</roll>
        <roll>content here</roll>
    </student>
    <student>
        <roll>content here</roll>
        <roll>content here</roll>
        <roll>content here</roll>
        <roll>content here</roll>
    </student>
    </kita>
</kk>

XSL文件:

在xsl中有更多的代码,我只是复制到这里:

代码语言:javascript
复制
<xsl:if test="parent::student[parent::kita]/following-sibling::student/x:roll[@more]">
    <more/>
</xsl:if>

预期输出:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<kk>
    <kita>
    <student>
        <roll>content here</roll>
        <roll>content here</roll>
        <roll>content here</roll>
        <roll more="1">content here</roll>
    </student>
    <student>
        <!-- if @more="1" or @more="2" add <more> element-->
        <more>Content here</more>
        <roll>content here</roll>
        <roll>content here</roll>
        <roll>content here</roll>
        <roll>content here</roll>
    </student>
    <student>
        <!-- If @more="2" add <more> element below-->
        <more>Content here</more>
        <roll>content here</roll>
        <roll>content here</roll>
        <roll>content here</roll>
        <roll>content here</roll>
    </student>
    </kita>
</kk>
EN

回答 1

Stack Overflow用户

发布于 2021-10-19 13:47:53

试着沿着这样的思路

代码语言:javascript
复制
  <xsl:template match="kita">
    <xsl:copy>
      <xsl:for-each-group select="student" group-starting-with="student[roll/@more]">
        <xsl:apply-templates select="current-group()">
          <xsl:with-param name="emit-more" select="xs:integer(roll/@more)"/>
          <xsl:with-param name="more" select="roll[@more]"/>
        </xsl:apply-templates>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="student">
    <xsl:param name="emit-more"/>
    <xsl:param name="more"/>
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:if test="position() - 1 = (1 to $emit-more)">
        <more>
          <xsl:apply-templates select="$more/node()"/>
        </more>
      </xsl:if>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69629668

复制
相关文章

相似问题

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