Search code examples
xmlxsltxslt-2.0xslt-grouping

XSLT Grouping in Matching Substring


I'm doing a unparsed-text() for an xslt. So the data is coming in from a text file

I need to get a closing tag of element thistagwithsomeattributes as part of a group in the matching-string as follows:

      <xsl:template  match="/" name="text2xml">
      <xsl:variable name="entries" as="node()*">
        <xsl:analyze-string select="$textfile" regex="\r\n?|\n">
            <xsl:non-matching-substring>
              <xsl:analyze-string select="." regex="someregcode">
        <xsl:matching-substring>
          <group>
           <thistagwithsomeattributes>
             <abc>some value coming from regex</abc>
             <cde>some value from regex</cde>
           </thistagwithsomeattributes>
          </group>
         </xsl:matching-substring>
      </xsl:analyze-string>
            </xsl:non-matching-substring>
          </xsl:analyze-string>     
    </xsl:variable>

 <xsl:for-each-group select="$entries" group-by="abc">
        <final>
         <xsl:copy-of 
            select="current-group()[1]/abc,current-group()/*[not(self::abc)]"/>

        </final>
      </xsl:for-each-group>      

THe above doesn't work for me. If I remove the element thistagwithsomeattributes, then it works. How do I get this element as part of the group in the matching substring. I need this because the attributes for this element will have different values for the group

What I'm trying to achieve (following XML structure):

<thistagwithsomeattributes  att1="fromregexwithinggroup" att2="fromregexwithinggrouop">
<someelement>
<anothertagwithattributes att1="fromregexvalues">
 <cont>
     <itm>
      abc>some value coming from regex</abc>
     <cde>some value from regex</cde>
     </itm>
 </cont>
</anothertagwithattributes>
</thistagwithsomeattributes>

Now when I enter <thistagwithsomeattributes> and <anothertagwithattributes>, it doesn't work (no results), xml file is empty. But when I remove them. I get the following results:


Solution

  • You need to adapt the <xsl:for-each-group select="$entries" to <xsl:for-each-group select="$entries/thistagwithsomeattributes".