2

Using XSLT 1.0 - I have the following xml and I an trying to perform the following

  • Group By the first Field where id="1923" if the value attribute is the same
  • and average all the fields with id="3095" using the value attribute
  • and average all the fields with id="3095" using the value attribute

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="Trans.xsl"?>
 <Results>
  <List count="6">
   <Record contentId="1017835" finalFlag="True" levelId="" moduleId="152" primarySortId="" secondarySortId="" parentId="0" row="1" parentRow="" >
    <Field id="1923" type="1" value="Test 1"></Field>
    <Field id="3095" type="4" valueID="3809" value="1" parentId="3809" parentName="5" ></Field>
    <Field id="3096" type="4" valueID="3809" value="1" parentId="3809" parentName="5" ></Field>
   </Record>
   <Record contentId="1017828" finalFlag="True" levelId="" moduleId="152" primarySortId="" secondarySortId="" parentId="0" row="2" parentRow="" >
    <Field id="1923" type="1" value="Test 2"></Field>
    <Field id="3095" type="4" valueID="729" value="2" parentId="729" parentName="2" ></Field>
    <Field id="3096" type="4" valueID="3809" value="5" parentId="3809" parentName="5" ></Field>
   </Record>
   <Record contentId="1017978" finalFlag="True" levelId="" moduleId="152" primarySortId="" secondarySortId="" parentId="0" row="3" parentRow="" >
    <Field id="1923" type="1" value="Test 3"></Field>
    <Field id="3095" type="4" valueID="3808" value="4" parentId="3808" parentName="4" ></Field>
    <Field id="3096" type="4" valueID="3808" value="4" parentId="3808" parentName="4" ></Field>
   </Record>
   <Record contentId="1035463" finalFlag="True" levelId="" moduleId="152" primarySortId="" secondarySortId="" parentId="0" row="4" parentRow="" >
    <Field id="1923" type="1" value="Test 2"></Field>
    <Field id="3095" type="4" valueID="3808" value="4" parentId="3808" parentName="4" ></Field>
    <Field id="3096" type="4" valueID="730" value="3" parentId="730" parentName="3" ></Field>
   </Record>
   <Record contentId="1017985" finalFlag="True" levelId="" moduleId="152" primarySortId="" secondarySortId="" parentId="0" row="5" parentRow="" >
    <Field id="1923" type="1" value="Test 1"></Field>
    <Field id="3095" type="4" valueID="113690" value="10" parentId="113690" parentName="10" ></Field>
    <Field id="3096" type="4" valueID="113690" value="10" parentId="113690" parentName="10" ></Field>
   </Record>
   <Record contentId="1017835" finalFlag="True" levelId="" moduleId="152" primarySortId="" secondarySortId="" parentId="0" row="6" parentRow="" >
    <Field id="1923" type="1" value="Test 1"></Field>
    <Field id="3095" type="4" valueID="3809" value="5" parentId="3809" parentName="5" ></Field>
    <Field id="3096" type="4" valueID="3809" value="5" parentId="3809" parentName="5" ></Field>
   </Record>
  </List>
 </Results>

Trying to produce the following using trans.xsl:

<Records>
 <Data>
  <Text1923>Test 1</Text1923>
  <Avg3095>5.33</Avg3095>
  <Avg3096>5.33</Avg3096>
 </Data>
 <Data>
  <Text1923>Test 2</Text1923>
  <Avg3095>3</Avg3095>
  <Avg3096>4</Avg3096>
 </Data>
 <Data>
  <Text1923>Test 3</Text1923>
  <Avg3095>4</Avg3095>
  <Avg3096>4</Avg3096>
 </Data>
</Records>
5
  • And what technology are you using to work with your XML? Xsl? Java? .NET? Something else?
    – Oded
    Commented May 9, 2010 at 16:42
  • I am using .NET 2.0 and trying to transform the above xml to generate a dashboard showing key text and averages
    – Wail
    Commented May 9, 2010 at 16:54
  • The transformation is based on an xsl document that I am trying to create
    – Wail
    Commented May 9, 2010 at 16:57
  • The XML is generated dynamically via a web service and the only fixed data attributes are the ids and types in the first xml structure so I can't really depend on the value attribute to test its content since it is changing
    – Wail
    Commented May 9, 2010 at 16:59
  • Good Question (+1)! See my answer for a complete and efficient solution. :) Commented May 9, 2010 at 17:22

1 Answer 1

2

Here is a simple XSLT 1.0 solution using the Muenchian Grouping Method:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kFieldById" match="Field"
  use="@id"/>

 <xsl:key name="kField1923"
  match="Field[@id='1923']" use="@value"/>

 <xsl:template match="/">
  <Records>
    <xsl:for-each select=
    "key('kFieldById', '1923')
         [generate-id()
         =
          generate-id(key('kField1923',
                          @value
                          )
                           [1]
                      )
          ]
    ">
     <Data>
         <Text1923>
           <xsl:value-of select="@value"/>
         </Text1923>

         <xsl:variable name="vSubfield3095" select=
           "key('kField1923',@value)
                         /../Field[@id='3095']
           "/>

         <xsl:variable name="vSubfield3096" select=
           "key('kField1923',@value)
                         /../Field[@id='3096']
           "/>

         <Avg3095>
           <xsl:value-of select=
            "sum($vSubfield3095/@value)
            div
             count($vSubfield3095)
            "/>
         </Avg3095>

         <Avg3096>
           <xsl:value-of select=
            "sum($vSubfield3096/@value)
            div
             count($vSubfield3096)
            "/>
         </Avg3096>
     </Data>
    </xsl:for-each>
  </Records>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document, the wanted, correct resul is produced:

<Records>
   <Data>
      <Text1923>Test 1</Text1923>
      <Avg3095>5.333333333333333</Avg3095>
      <Avg3096>5.333333333333333</Avg3096>
   </Data>
   <Data>
      <Text1923>Test 2</Text1923>
      <Avg3095>3</Avg3095>
      <Avg3096>4</Avg3096>
   </Data>
   <Data>
      <Text1923>Test 3</Text1923>
      <Avg3095>4</Avg3095>
      <Avg3096>4</Avg3096>
   </Data>
</Records>
2
  • @Wail: Glad it was useful. How about upvoting + accepting the answer? :) Commented May 9, 2010 at 17:59
  • Accpeted answer and not sure why it does not allow me to up the vote for you . ANy ideas and I will be glad to up the vote perhapse I do not have enough points to vote?
    – Wail
    Commented May 10, 2010 at 0:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.