Javascript 使用XSLT获取剩余部分

Javascript 使用XSLT获取剩余部分,javascript,html,xslt-1.0,xslt-2.0,xslt-3.0,Javascript,Html,Xslt 1.0,Xslt 2.0,Xslt 3.0,我第一次写这篇文章是在xslt中(可以是XSLT2或3)。我需要所有余款的总和。有人已经遇到过这种情况吗?多谢各位 以下是要求: 2 IDs fields called id1 and id2. Each field has 6 chars (incl. space and decimal) LEFT ALIGN: id1: 111 (3 spaces included here after 111) RIGHT ALIGN: id2: 123.50 (one dec

我第一次写这篇文章是在xslt中(可以是XSLT2或3)。我需要所有余款的总和。有人已经遇到过这种情况吗?多谢各位

以下是要求:

2 IDs fields called id1 and id2. Each field has 6 chars (incl. space and decimal)

LEFT ALIGN:
    id1: 111   (3 spaces included here after 111) 

RIGHT ALIGN:
    id2: 123.50  (one decimal here)
我需要得到第一个值A,B,C,D,然而:

    VALUE A: add up id1 from position 1 - 3
    VALUE B: add up id1 from position 4 - 6

    VALUE C: add up id2 from position 1 - 3
    VALUE D: add up id2 from position 4 - 6
注:空格的值为20,小数点“.”为15

样本计算

id1: 111   (3 spaces included here)
    VALUE A: 3 (1+1+1) AND
    VALUE B: 60 (20+20+20)

id2: 123.50  (one decimal here)
    VALUE C: 5 (1+2+3) AND
    VALUE D: 20 (15+5)
Remainder= mod(abs((B+D)-(A+C)),13)

7 

Sum of all remainders: 7


Example 2:

Row 1:
LEFT ALIGN:
id1: 111   (3 spaces included here after 111) 

RIGHT ALIGN
id2: 123.50  

Row 2:
LEFT ALIGN:
id1: 321.50   

RIGHT ALIGN
id2:   222  (3 spaces before 111)

Row 1:
Value A: 3 (1+1+1)
Value B: 60 (20+20+20)
Value C: 6 (1+2+3)
Value D: 20 (15+5)

Row 2:
Value A: 6 (3+2+1)
Value B: 20 (15+5)
Value C:60 (20+20+20 
Value D: 6 (2+2+2)

Remainder= mod(abs((B+D)-(A+C)),13)

Row1: Remainder = mod(abs((60+20)-(3+6)),13)
Remainder = 6

Row 2:Remainder=mod(abs((20+6)-(6+6)),13)
Remainder = 1


Sum of all remainders: 7 (6+1) 

7 or the sum of all remainders will be included in the final output file.

感谢您的帮助。

对于XSLT 3,一种方法可能是使用累加器:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:mf="http://example.com/mf"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:output method="text" item-separator="&#10;"/>

  <xsl:mode on-no-match="shallow-skip" use-accumulators="#all"/>

  <xsl:accumulator name="A" as="xs:integer?" initial-value="()">
      <xsl:accumulator-rule
        match="fields/data/id1"
        phase="end"
        select="accumulator-after('values') => subsequence(1, 3) => sum()"/>
  </xsl:accumulator>

  <xsl:accumulator name="B" as="xs:integer?" initial-value="()">
      <xsl:accumulator-rule
        match="fields/data/id1"
        phase="end"
        select="accumulator-after('values') => subsequence(4, 3) => sum()"/>
  </xsl:accumulator>

  <xsl:accumulator name="C" as="xs:integer?" initial-value="()">
      <xsl:accumulator-rule
        match="fields/data/id2"
        phase="end"
        select="accumulator-after('values') => subsequence(1, 3) => sum()"/>
  </xsl:accumulator>

  <xsl:accumulator name="D" as="xs:integer?" initial-value="()">
      <xsl:accumulator-rule
        match="fields/data/id2"
        phase="end"
        select="accumulator-after('values') => subsequence(4, 3) => sum()"/>
  </xsl:accumulator>

  <xsl:accumulator name="values" as="xs:integer*" initial-value="()">
      <xsl:accumulator-rule
        match="data" select="()"/>
      <xsl:accumulator-rule
        match="data/id1/text() | data/id2/text()"
        select="analyze-string(.,'.')//*:match ! (
                  if (. = '.')
                  then 15
                  else if (. = ' ')
                  then 20
                  else xs:integer(.))"/>
  </xsl:accumulator>

  <xsl:accumulator name="remainder" as="xs:integer?" initial-value="()">
      <xsl:accumulator-rule
        match="fields/data" select="()"/>
      <xsl:accumulator-rule
        match="fields/data"
        phase="end"
        select="abs((accumulator-after('B') + accumulator-after('D')) -
                     (accumulator-after('A') + accumulator-after('C'))) mod 13"/>
  </xsl:accumulator>

  <xsl:accumulator name="remainder-sum" as="xs:integer?" initial-value="()">
      <xsl:accumulator-rule
        match="fields" select="0"/>
      <xsl:accumulator-rule
        match="fields/data"
        select="$value + accumulator-after('remainder')"/>
  </xsl:accumulator>


  <xsl:output method="text" item-separator="&#10;"/>

  <xsl:template match="fields">
      <xsl:apply-templates/>
      <xsl:sequence
        select="accumulator-after('remainder-sum')"/>
  </xsl:template>

</xsl:stylesheet>

当然,您只能将代码应用于具有

  <xsl:template match="fields[last()]">
      <xsl:apply-templates/>
      <xsl:sequence
        select="accumulator-after('remainder-sum')"/>
  </xsl:template>


(而不是先前使用的
)。请参见

为什么您有
值C:5(1+2+3)
,这不应该是
6
?您忘记提供源XML文档了吗。。。你好,马丁,非常感谢。根据您的代码,它将返回一个总计序列(例如6 7)是否有一种方法可用于获取最后一个序列或总计?(例如,我只需要7)谢谢您Hi@Martin,我知道您正在使用一个节点来汇总行值的值(例如,qnA,考虑问一个关于这个问题的新问题,在这里你显示你的输入,你想要的结果,你已经尝试过的XSLT,你得到的任何错误,都被减少到最小,但是完整的样本来证明这个问题。如果问题是对输入进行预处理,使它变成一种格式,你可以使用这个答案的代码。然后简单地编写一个不同的模式或不同的样式表,首先根据需要添加空格或格式。如果您需要帮助,请单独提问。