XSLT当找到字符串时来自不同节点的连接

huangapple go评论130阅读模式
英文:

XSLT Concatenation from different nodes when a string is found

问题

我想要连接不同的节点从"Time"直到在"Sum"中提供另一个数据。大多数sum节点都是空的。我想要将下一个SUM值之前的所有节点连接到一个单一的引用字段中。

<Reference>Debit,TMU/2023/1059 A ,TMU/2023/1019 A ,TMU/2023/1020 A </Reference>
<Reference> Debit,56011846 A </Reference>

这是XML输入,其中包含SUM和TIME节点。大多数SUM节点都为空。我想要将下一个SUM值之前的所有节点连接到一个单一的引用字段中。

英文:

> I want to concatenate different nodes from "Time" until there is
> another data provided in "Sum". Most of the sum nodes are empty. I
> will like to concatenate the all nodes before the next SUM value to a
> single reference field.

    &lt;?xml version=&quot;1.0&quot;?&gt;
    &lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
    	&lt;!-- recursive named template --&gt;
    	&lt;xsl:template name=&quot;hm&quot;&gt;
    		&lt;xsl:param name=&quot;amount&quot;/&gt;
    		&lt;xsl:param name=&quot;pos&quot;/&gt;
    		&lt;xsl:for-each select=&quot;../row&quot;&gt;
    			&lt;xsl:if test=&quot;$amount = Sum and $pos = position()&quot;&gt;&lt;/xsl:if&gt;
    			&lt;xsl:choose&gt;
    				&lt;xsl:when test=&quot;string-length(Sum) &amp;gt; 0&quot;&gt;
    					&lt;xsl:value-of select=&quot;Time&quot;/&gt;
    				&lt;/xsl:when&gt;
    				&lt;xsl:otherwise&gt;
    					&lt;xsl:value-of select=&quot;&#39;, &#39;&quot;/&gt;
    					&lt;xsl:value-of select=&quot;Time&quot;/&gt;
    				&lt;/xsl:otherwise&gt;
    			&lt;/xsl:choose&gt;
    		&lt;/xsl:for-each&gt;
    	&lt;/xsl:template&gt;
    						&lt;Reference&gt;
    							&lt;xsl:call-template name=&quot;hm&quot;&gt;
    								&lt;xsl:with-param name=&quot;amount&quot; select=&quot;Sum&quot;/&gt;
    								&lt;xsl:with-param name=&quot;pos&quot; select=&quot;position()&quot;/&gt;
    							&lt;/xsl:call-template&gt;
    						&lt;/Reference&gt;
    					&lt;/Payment&gt;
    				&lt;/xsl:if&gt;
    			&lt;/xsl:for-each&gt;
    		&lt;/Trailer&gt;
    	&lt;/xsl:template&gt;
    &lt;/xsl:stylesheet&gt;

> > # # <!--This is the output which gives concatenation of all the nodes instead of the nodes before the next sum-->

 &lt;Trailer&gt; 
             &lt;Payment&gt;  
              &lt;Reference&gt;Debit,TMU/2023/1059 A ,TMU/2023/1019 A ,TMU/2023/1020 A, Debit,56011846 A 
    &lt;/Reference&gt; 
              &lt;/Payment&gt; 
              &lt;Payment&gt; 
               &lt;Reference&gt;Debit,TMU/2023/1059 A ,TMU/2023/1019 A ,TMU/2023/1020 A, Debit,56011846 A 
    &lt;/Reference&gt; 
               &lt;/Payment&gt;
        &lt;/Trailer&gt; 

>
> # <!--But my expected output is with the value of all the TIME nodes before the next SUM that has a value-->

    &lt;Trailer&gt; 
    &lt;Payment&gt; 
     &lt;Reference&gt;Debit,TMU/2023/1059 A ,TMU/2023/1019 A ,TMU/2023/1020 A &lt;/Reference&gt; 
    &lt;/Payment&gt; 
    &lt;Payment&gt; 
     &lt;Reference&gt; Debit,56011846 A &lt;/Reference&gt;
    &lt;/Payment&gt;
    &lt;/Trailer&gt;

> > #This is the xml input which has the nodes SUM and TIME. Most of the sum nodes are empty. I will like to concatenate the all nodes
> before the next SUM value to a single reference field.

&lt;root&gt;
        &lt;row&gt;
             &lt;Time&gt;Debit&lt;/Time&gt;
             &lt;Sum&gt;(874,516.25)&lt;/Sum&gt;
       &lt;/row&gt;
        &lt;row&gt;
               &lt;Time&gt;TMU/2023/1059 A&lt;/Time&gt; 
               &lt;Sum&gt;&lt;/Sum&gt; 
      &lt;/row&gt;
       &lt;row&gt; 
              &lt;Time&gt;TMU/2023/1019 A&lt;/Time&gt;
               &lt;Sum&gt;&lt;/Sum&gt;
        &lt;/row&gt;
         &lt;row&gt;
           &lt;Time&gt;TMU/2023/1020 A&lt;/Time&gt;
           &lt;Sum&gt;&lt;/Sum&gt;
         &lt;/row&gt;
&lt;row&gt;
        &lt;Time&gt;Debit&lt;/Time&gt;
      &lt;Sum&gt;(664,297.52)&lt;/Sum&gt; 
     &lt;/row&gt;
     &lt;row&gt;
            &lt;Time&gt;56011846 A&lt;/Time&gt;
             &lt;Sum&gt;&lt;/Sum&gt;
             &lt;/row&gt;
&lt;/root&gt;

答案1

得分: 0

如我在评论中提到的,这似乎是一个分组问题。如果(如您所报告)您的处理器支持XSLT 2.0,您应该能够使用以下代码生成预期的输出:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/root">
    <Trailer> 
        <xsl:for-each-group select="row" group-starting-with="row[Sum/text()]">
            <Payment>  
                <Reference>
                    <xsl:value-of select="current-group()/Time" separator=","/>
                </Reference> 
            </Payment>
        </xsl:for-each-group>
    </Trailer> 
</xsl:template>

</xsl:stylesheet>
英文:

As I mentioned in the comments, this seems to be a grouping question. If (as you report) your processor supports XSLT 2.0, you should be able to produce the expected output using:

XSLT 2.0

&lt;xsl:stylesheet version=&quot;2.0&quot; 
xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
&lt;xsl:output method=&quot;xml&quot; version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; indent=&quot;yes&quot;/&gt;

&lt;xsl:template match=&quot;/root&quot;&gt;
	&lt;Trailer&gt; 
		&lt;xsl:for-each-group select=&quot;row&quot; group-starting-with=&quot;row[Sum/text()]&quot;&gt;
			&lt;Payment&gt;  
				&lt;Reference&gt;
					&lt;xsl:value-of select=&quot;current-group()/Time&quot; separator=&quot;,&quot;/&gt;
				&lt;/Reference&gt; 
			&lt;/Payment&gt;
		&lt;/xsl:for-each-group&gt;
	&lt;/Trailer&gt; 
&lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;

huangapple
  • 本文由 发表于 2023年6月13日 05:59:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460578.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定