简体   繁体   中英

How to read 2 comma separated string in single loop - XSLT

I have below two variables which have multiple commas separated string values in different XSLT variable

Variable_01 : 88888,777777
Variable_02 : abc,xyz

Now I am looking for below output

[{"Group":"88888", "Name":"abc"},{"Group":"777777", "Name":"xyz"}]

Could you please help me what is correct XSLT code for the above output.

Here's one way you could look at it:

XSLT 2.0

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

<xsl:variable name="Variable_01">88888,777777</xsl:variable>
<xsl:variable name="Variable_02">abc,xyz</xsl:variable>

<xsl:template match="/">
    <xsl:text>[</xsl:text>
    <xsl:for-each select="tokenize($Variable_01, ',')">
        <xsl:variable name="i" select="position()"/>
        <xsl:text>{"Group":"</xsl:text>
        <xsl:value-of select="." />
        <xsl:text>", "Name":"</xsl:text>
        <xsl:value-of select="tokenize($Variable_02, ',')[$i]" />
        <xsl:text>"}</xsl:text>
        <xsl:if test="position()!=last()">
            <xsl:text>,</xsl:text>
        </xsl:if>
    </xsl:for-each>
    <xsl:text>]</xsl:text>
</xsl:template>

</xsl:stylesheet>

Result

[{"Group":"88888", "Name":"abc"},{"Group":"777777", "Name":"xyz"}]

Demo : http://xsltransform.hikmatu.com/jyH9rLV

In XSLT 3 with higher-order function for-each-pair and JSON map and array support this would be as easy as

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:array="http://www.w3.org/2005/xpath-functions/array"
    xmlns:mf="http://example.com/mf"
    exclude-result-prefixes="#all"
    version="3.0">

    <xsl:output method="json" indent="yes"/>

    <xsl:param name="Variable_01" as="xs:string">88888,777777</xsl:param>
    <xsl:param name="Variable_02" as="xs:string">abc,xyz</xsl:param>

    <xsl:template match="/" name="xsl:initial-template">
        <xsl:sequence 
         select="array { 
                   for-each-pair(
                     tokenize($Variable_01, ','),
                     tokenize($Variable_02, ','),
                     function($a, $b) { map { 'Group' : $a, 'Name' : $b } }
                   )
                 }"/>
    </xsl:template>

</xsl:stylesheet>

In XSLT 3 without higher-order function support for for-each-pair you could implement your own function along the lines of the defintion:

<?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:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:array="http://www.w3.org/2005/xpath-functions/array"
    xmlns:mf="http://example.com/mf"
    exclude-result-prefixes="#all"
    version="3.0">

    <xsl:output method="json" indent="yes"/>

    <xsl:function name="mf:object-for-each-pair" as="map(xs:string, xs:string)*">
      <xsl:param name="seq1"/>
      <xsl:param name="seq2"/>
      <xsl:param name="action"/>
      <xsl:if test="exists($seq1) and exists($seq2)">
        <xsl:sequence select="map { 'Group' : head($seq1), 'Name' : head($seq2) }"/>
        <xsl:sequence select="mf:object-for-each-pair(tail($seq1), tail($seq2))"/>
      </xsl:if>
    </xsl:function>

    <xsl:param name="Variable_01" as="xs:string">88888,777777</xsl:param>
    <xsl:param name="Variable_02" as="xs:string">abc,xyz</xsl:param>

    <xsl:template match="/" name="xsl:initial-template">
        <xsl:sequence select="array { mf:object-for-each-pair(tokenize($Variable_01, ','), tokenize($Variable_02, ',')) }"/>
    </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/6qVRKxk

Finally in XSLT 2 without JSON array and map support you could use the same approach to create text output instead:

<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="2.0">

    <xsl:output method="text"/>

    <xsl:function name="mf:object-for-each-pair" as="xs:string*">
      <xsl:param name="seq1"/>
      <xsl:param name="seq2"/>
      <xsl:if test="exists($seq1) and exists($seq2)">
        <xsl:sequence select="concat('{ &quot;Group&quot; : &quot;', $seq1[1], '&quot;, &quot;Name&quot; : &quot;', $seq2[1], '&quot; }')"/>
        <xsl:sequence select="mf:object-for-each-pair(subsequence($seq1, 2), subsequence($seq2, 2))"/>
      </xsl:if>
    </xsl:function>

    <xsl:param name="Variable_01" as="xs:string">88888,777777</xsl:param>
    <xsl:param name="Variable_02" as="xs:string">abc,xyz</xsl:param>

    <xsl:template match="/">
        <xsl:text>[ </xsl:text>
        <xsl:value-of select="mf:object-for-each-pair(tokenize($Variable_01, ','), tokenize($Variable_02, ','))" separator=", "/>
        <xsl:text> ]</xsl:text>
    </xsl:template>

</xsl:stylesheet>

http://xsltransform.hikmatu.com/nc4NzPX

This is somewhat crude. But it gets the job done.

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="text" encoding="UTF-8"/>

    <xsl:variable name="Variable_01">88888,777777</xsl:variable>
    <xsl:variable name="Variable_02">abc,xyz</xsl:variable>

    <xsl:template match="/">
        <xsl:text>[</xsl:text>
        <xsl:call-template name="tokenizeString">
            <!-- store anything left in another variable -->
            <xsl:with-param name="list" select="$Variable_01"/>
            <xsl:with-param name="list2" select="$Variable_02"/>
            <xsl:with-param name="delimiter" select="','"/>
        </xsl:call-template>
        <xsl:text>]</xsl:text>
    </xsl:template>

    <xsl:template name="tokenizeString">
        <!--passed template parameter -->
        <xsl:param name="list"/>
        <xsl:param name="list2"/>
        <xsl:param name="delimiter"/>
        <xsl:choose>
            <xsl:when test="contains($list, $delimiter)">
                <!-- get everything in front of the first delimiter -->
                <xsl:text>{"Group":"</xsl:text>
                <xsl:value-of select="substring-before($list,$delimiter)"/>
                <xsl:text>", "Name":"</xsl:text>
                <xsl:call-template name="tokenizeAnother">
                    <xsl:with-param name="list2" select="$list2"/>
                    <xsl:with-param name="delimiter" select="$delimiter"/>
                </xsl:call-template>
                <xsl:text>"},</xsl:text>
                <xsl:call-template name="tokenizeString">
                    <!-- store anything left in another variable -->
                    <xsl:with-param name="list" select="substring-after($list,$delimiter)"/>
                    <xsl:with-param name="list2" select="substring-after($list2,$delimiter)"/>
                    <xsl:with-param name="delimiter" select="$delimiter"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>{"Group":"</xsl:text>
                <xsl:value-of select="$list"/>
                <xsl:text>", "Name":"</xsl:text>
                <xsl:call-template name="tokenizeAnother">
                    <xsl:with-param name="list2" select="$list2"/>
                    <xsl:with-param name="delimiter" select="$delimiter"/>
                </xsl:call-template>
                <xsl:text>"}</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name="tokenizeAnother">
        <xsl:param name="list2"/>
        <xsl:param name="delimiter"/>
        <xsl:choose>
            <xsl:when test="contains($list2, $delimiter)">
                <!-- get everything in front of the first delimiter -->
                <xsl:value-of select="substring-before($list2,$delimiter)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$list2"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

See it in action here .

For completion, here's a purely XSLT 1.0 solution that does not require any extension functions:

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

<xsl:variable name="Variable_01">88888,777777</xsl:variable>
<xsl:variable name="Variable_02">abc,xyz</xsl:variable>

<xsl:template match="/">
    <xsl:text>[</xsl:text>
    <xsl:call-template name="tokenize-to-pairs">
        <xsl:with-param name="left-text" select="$Variable_01"/>
        <xsl:with-param name="right-text" select="$Variable_02"/>
    </xsl:call-template>
    <xsl:text>]</xsl:text>
</xsl:template>

<xsl:template name="tokenize-to-pairs">
    <xsl:param name="left-text"/>
    <xsl:param name="right-text"/>
    <xsl:param name="delimiter" select="','"/>
    <!-- output -->
    <xsl:text>{"Group":"</xsl:text>
    <xsl:value-of select="substring-before(concat($left-text, $delimiter), $delimiter)" />
    <xsl:text>", "Name":"</xsl:text>
    <xsl:value-of select="substring-before(concat($right-text, $delimiter), $delimiter)" />
    <xsl:text>"}</xsl:text>
    <!-- recursive call -->
    <xsl:if test="contains($left-text, $delimiter)">
        <xsl:text>,</xsl:text>
        <xsl:call-template name="tokenize-to-pairs">
            <xsl:with-param name="left-text" select="substring-after($left-text, $delimiter)"/>
            <xsl:with-param name="right-text" select="substring-after($right-text, $delimiter)"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM