首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何计算以下兄弟姐妹::*[1][self::disp-引号]

如何计算以下兄弟姐妹::*[1][self::disp-引号]
EN

Stack Overflow用户
提问于 2020-01-31 13:42:31
回答 2查看 62关注 0票数 0

如何计算following-sibling::*[1][self::disp-quote]位置示例 S/B 示例1

输入文件

代码语言:javascript
复制
    <?xml version="1.0" encoding="UTF-8"?>
<sec>
    <title>Title</title>
    <p>vlvvnlfjkvv</p>
    <disp-quote><p content-type="example"><bold>EXAMPLE</bold> aaaaaaa</p></disp-quote>
    <disp-quote><p content-type="example"><bold>EXAMPLE</bold> aaaaaaa</p></disp-quote>
    <disp-quote><p content-type="example"><bold>EXAMPLE</bold> aaaaaaa</p></disp-quote>
    <p>aaaaaa</p>
    <p>aaaaaa</p>
    <disp-quote><p content-type="example"><bold>EXAMPLE</bold> aaaaaaa</p></disp-quote>
    <p>vvvkvuvhv</p>
</sec>

预期输出

代码语言:javascript
复制
    <?xml version="1.0" encoding="UTF-8"?>
<sec>
    <title>Title</title>
    <p>vlvvnlfjkvv</p>
    <disp-quote><p content-type="example"><bold>EXAMPLE 1</bold> aaaaaaa</p></disp-quote>
    <disp-quote><p content-type="example"><bold>EXAMPLE 2</bold> aaaaaaa</p></disp-quote>
    <disp-quote><p content-type="example"><bold>EXAMPLE 3</bold> aaaaaaa</p></disp-quote>
    <p>aaaaaa</p>
    <p>aaaaaa</p>
    <disp-quote><p content-type="example"><bold>EXAMPLE</bold> aaaaaaa</p></disp-quote>
    <p>vvvkvuvhv</p>
</sec>

XSLT

代码语言:javascript
复制
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:template match="bold">
        <bold aid:cstyle="bold">
            <xsl:apply-templates />
            <xsl:if test=".='EXAMPLE' and ancestor::disp-quote[following-sibling::*[1][self::disp-quote]]">
                <xsl:for-each select="ancestor::disp-quote[following-sibling::*[1][self::disp-quote]]">
                    <xsl:value-of select="count(ancestor::disp-quote[following-sibling::*[1][self::disp-quote]])+1"/>
                </xsl:for-each>
            </xsl:if>
        </bold>
    </xsl:template>
</xsl:stylesheet>

从注释编辑

我的预期结果,只有当它与相同的磁盘报价相同时,然后添加位置()示例1,示例2,示例3。如果它是单个磁盘引用,那么只需更改示例。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-31 18:18:49

我认为Michael关于使用xsl:number的建议是您希望使用的方式,只要您希望在任何disp-quote中对所有的disp-quote进行编号,即使是一个单独的;如果您只想在相邻的组中有多个disp-quote时对它们进行编号,那么在评论中提出的建议可能会有帮助:

代码语言:javascript
复制
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output indent="yes"/>

  <xsl:template match="sec[disp-quote]">
      <xsl:copy>
          <xsl:for-each-group select="*" group-adjacent="boolean(self::disp-quote[p/bold = 'EXAMPLE'])">
              <xsl:choose>
                  <xsl:when test="current-grouping-key() and tail(current-group())">
                      <xsl:apply-templates select="current-group()" mode="number-examples"/>
                  </xsl:when>
                  <xsl:otherwise>
                      <xsl:apply-templates select="current-group()"/>
                  </xsl:otherwise>
              </xsl:choose>
          </xsl:for-each-group>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="disp-quote" mode="number-examples">
      <xsl:copy>
          <xsl:apply-templates>
              <xsl:with-param name="dq-pos" tunnel="yes" select="position()"/>
          </xsl:apply-templates>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="disp-quote/p/bold[. = 'EXAMPLE']">
      <xsl:param name="dq-pos" tunnel="yes" select="()"/>
      <xsl:copy>
          <xsl:value-of select="., $dq-pos"/>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bwe3bW/

票数 1
EN

Stack Overflow用户

发布于 2020-01-31 17:22:46

你把这件事弄得很复杂(也许真的是这样?)

我会做

代码语言:javascript
复制
<xsl:template match="disp-quote//bold[.='EXAMPLE']">
  <bold aid:cstyle="bold">
    <xsl:text>EXAMPLE </xsl:text>
    <xsl:number level="any" count="bold" from="sec"/>
  </bold>
</xsl:template>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60005072

复制
相关文章

相似问题

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