Home > Back-end >  Unable to get the element values dynamically using xslt inside for-each-group , group-starts-with an
Unable to get the element values dynamically using xslt inside for-each-group , group-starts-with an

Time:03-17

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>

CodePudding user response:

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>
  • Related