如何为 XSLT 中的两组元素生成相同的随机数?

How to generate random number which is same for two set of elements in XSLT?

输入:

<paragraph>
      <bq>34152</bq>
      <eq>52325/eq>
      <private type="DOB">SP1</private>
      <bq>12431</bq>
      <abc>EX</abc>
      <eq>31333</eq>
</paragraph>

预期输出:

<paragraph>
          <bq>55124</bq>
          <eq>55124</eq>
          <private type="DOB">SP1</private>
          <bq>66565</bq>
          <abc>EX</abc>
          <eq>66565</eq>
</paragraph>

如何使用应用模板为每个 bq 和 eq 集生成相同的随机数?

在 XSLT 3 中(例如 random-number-generator)它会归结为

  <xsl:accumulator name="rng" as="map(*)" initial-value="random-number-generator(current-dateTime())">
    <xsl:accumulator-rule match="paragraph/bq" select="$value?next()"/>
  </xsl:accumulator>
  
  <xsl:template match="paragraph/bq | paragraph/eq" expand-text="yes">
    <xsl:copy>{round(accumulator-before('rng')?number * 100000)}</xsl:copy>
  </xsl:template>

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

这需要 Saxon HE 10 或更高版本或 Saxon PE/EE 9.8 或更高版本或 Saxon-JS 2.