如何计算following-sibling::*[1][self::disp-quote]
位置示例 S / B 示例1
输入文件
<?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>
预期产量
<?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
<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>
编辑评论:
我的预期结果只有在同级disp-quote之后是disp-quote时才是相同的,然后使用position()进行添加。示例1,示例2,示例3。如果它是单disp-quote,则仅更改示例。
我认为迈克尔对使用的建议xsl:number
是你想要的,只要你想将所有编号使用方式<b>EXAMPLE</b>
中的任何disp-quote
,甚至一个单一的一个; 如果仅disp-quote
在相邻组中有多个的情况下仅对它们进行编号,则注释中的建议可能会有所帮助:
<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>
感谢@Martin Honnen的回复!
@Sandy,如果答复解决了您的问题,那么您可以通过接受答案来表示,那么很明显问题已解决。