Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

How to remove same number after tokenize use group-by in XSLT

I am trying to remove duplicate number after tokenize the value and another entry value e.g. <p>1(a), (b), (c)</p> and <p>1(a)</p>. How to remove duplicate entry using group-by.
Input XML

<root>
    <p>1(a), (b), (c)</p>
    <p>1(a)</p>
    <p>2(a)</p>
    <p>3(a)</p>
    <p>1(c)</p>
</root>

Expected Output

<root>
   <p>1(a)</p>
   <p>1(b)</p>
   <p>1(c)</p>
   <p>2(a)</p>
   <p>3(a)</p>
</root>

XSLT Code

<xsl:template match="root">
    <root>
        <xsl:for-each-group select="p" group-by=".">
            <xsl:sort select="current-grouping-key()" data-type="number" order="ascending"/>
            <xsl:choose>
                <xsl:when test="contains(current-grouping-key(), ', ')">
                    <xsl:variable name="tokens" select="tokenize(current-grouping-key(), ', and |, ')"/>
                    <xsl:for-each select="$tokens">
                        <p>
                            <xsl:value-of
                                select="if (starts-with(., '('))
                                then replace(head($tokens), '(.*)(.*?)$', '$1') || .
                                else ."/>
                        </p>
                        
                    </xsl:for-each>
                </xsl:when>
                <xsl:otherwise>
                    <p><xsl:value-of select="current-grouping-key()"/></p>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </root>
</xsl:template>
question from:https://stackoverflow.com/questions/65879616/how-to-remove-same-number-after-tokenize-use-group-by-in-xslt

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It might suffice to use

  <xsl:template match="root">
      <xsl:copy>
          <xsl:for-each 
            select="distinct-values(
                      p ! 
                      (let $tokens := tokenize(., 's*,s*') 
                           return (head($tokens), tail($tokens) ! (substring-before(head($tokens), '(') || .)))
                    )" expand-text="yes">
              <p>{.}</p>
          </xsl:for-each>
      </xsl:copy>
  </xsl:template>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...