2

Unable to get the values of elements which is having names with numbers in xsl

<UserDefinedFields>
  <UserField1>yui</UserField1>
  <UserField2>yui</UserField2>
 <UserField3>yui</UserField3>
 ..
 <UserField10>yui</UserField10>
</UserDefinedFields>

xslt which I tried is:

<xsl:for-each-group select="/UserDefinedFields/*" group-starts-with="UserField">
  <xsl:variable name="ind" select="position()"/>
  <xsl:element name="UDField$ind">
    <xsl:value-of select="/UserDefinedFields/concat('UserField',$ind})"/>
  </xsl:element>
</xsl:for-each-group>

Need below result:

 <UserDefinedFields>
      <UDField1>yui1</UDField1>
      <UDField2>yuiyh</UDField2>
     <UDField3>yuijk</UDField3>
     ..
     <UDField10>yuirt</UDField10>
    </UserDefinedFields>
user3898783
  • 207
  • 1
  • 4
  • 16

1 Answers1

0

In order to apply the value of that $ind number to the element name, you need to wrap it in curly braces, and Attribute Value Template:

<xsl:element name="UDField{$ind}">

Based upon the input XML and the snippet provided, it isn't clear why you need xsl:for-each-group. It seems that xsl:for-each with a predicate on the @select to select those child elements of UserDefinedFields who's names start with UserField should be sufficient:

<xsl:for-each select="/UserDefinedFields/*[starts-with(local-name(), 'UserField')]">
  <xsl:variable name="ind" select="position()"/>
  <xsl:element name="UDField{$ind}">
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:for-each>
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • Thank you so much. I am able to get the result. Could you please tell when we can use for-each-group – user3898783 Mar 16 '22 at 13:47
  • You are free to use it, if you want. But for this particular use case, it didn't seem that it would buy you much and would make the rest of the code a little more complicated. Typically you use `xsl:for-each-group` when you need to group records by something, to make it easier to calculate aggregate stats or generate grouped output (think of nested structures by the grouped-key, or table of contents) – Mads Hansen Mar 16 '22 at 14:32