0

i have the xml in a structure such as this:

<RWFCriteria reportType="ProgramReview">
  <item id="36" name="" value="9" type="Milestone" />
  <item id="31" name="" value="9" type="Milestone" />
  <item id="33" name="" value="11" type="Milestone" />
</RWFCriteria>

and need to it be converted to:

<data>
  <release id="9">  <milestone id="36" /> <milestone id="31" /> </release>
  <release id="11"> <milestone id="33" /> </release>
</data>

what would the XSLT look like for this transformation?

1 Answer 1

1

You need to group the items based on their value attribute. If you are using xslt 1 you can do this using the Muenchian Method which would look something like:

  <xsl:key name="item-value" match="item" use="@value" />

  <xsl:template match="/RWFCriteria">

    <data>
      <xsl:for-each select="item[count(. | key('item-value', @value)[1]) = 1]">
        <release id="{@value}">
          <xsl:for-each select="key('item-value', @value)">
            <milestone id="{@id}" />
          </xsl:for-each>
        </release>
      </xsl:for-each>
    </data>

  </xsl:template>
Sign up to request clarification or add additional context in comments.

3 Comments

@Alejandro Thanks for fixing the attributes
No problem. +1 For a correct answer. (Besides I don't like nested xsl:for-each)
@Alejandro Me neither, I would normally palm things off to their own template but I felt it was clearer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.