Link to home
Start Free TrialLog in
Avatar of Molko
Molko

asked on

XSLT Transformation Problem !

Hi
I am new to XSLT and i am having problems doing a translation. Can anyone help ?

This is a snapshot of my XML file that i which to transform.

-------------------------------------------------------------------------------------------------------------------------------------
<data name="list">
   <grid>
      <row>
         <data name="item" entity="entity-item" attribute="attribute-item" value="q100">
            <question id="q100" name="q100 name" position="1">
               <data>
                  <datum unit="MT1" reference="101" name="Amount" attribute="amount" value="£ 100.00"/>
                  <datum unit="MT1" reference="101" name="Age" attribute="ind" value="Yes"/>
               </data>
            </question>
            <oldData unit="TT1"/>
         </data>
         <data name="item" entity="entity-item" attribute="attribute-item" value="q100">
            <question id="q100" name="q100 name" position="2">
               <data>
                  <datum unit="MT1" reference="101" name="Amount" attribute="amount" value="£ 200.00"/>
                  <datum unit="MT1" reference="101" name="Age" attribute="ind" value="No"/>
               </data>
            </question>
            <oldData unit="TT1"/>
         </data>
         <data name="item" entity="entity-item" attribute="attribute-item" value="q100">
            <question id="q100" name="q100 name" position="3">
               <data>
                  <datum unit="MT1" reference="101" name="Amount" attribute="amount" value="£ 300.00"/>
                  <datum unit="MT1" reference="101" name="Age" attribute="ind" value="Yes"/>
               </data>
            </question>
            <oldData unit="TT1"/>
         </data>
         <data name="item" entity="entity-item" attribute="attribute-item" value="q100">
            <question id="q100" name="q100 name" position="4">
               <data>
                  <datum unit="MT1" reference="101" name="Amount" attribute="amount" value="£ 400.00"/>
                  <datum unit="MT1" reference="101" name="Age" attribute="ind" value="Yes"/>
               </data>
            </question>
            <oldData unit="TT1"/>
         </data>
        .....
        .....
        .....
      </row>
   </grid>
</data>


Now what i want to do is for every datum node with the a ' value="Yes" ', i want to
1) Update the <oldData unit="TT1"/>, so that the 'unit' = "TT2"
2) Update the <datum unit="MT1" reference="101" name="Amount" attribute="amount" value="£ 400.00"/>, so that the value of 'unit' it "MT2"
3) Finally i need to remove all <datum unit="MT1" reference="101" name="Age" attribute="ind" value="Yes"/>  regardless of value = yes or no.


I can do step one with the following XSLT, but am struggling with steps 2 and 3, can anyone help ?

-----------------------------------------------------------------------------
<!-- Update oldData attribute to TT2 -->
<xsl:template match="oldData[ancestor::data/question[@id='q100']//datum[@attribute='ind'][@value='Yes']]">
  <xsl:copy>
          <xsl:copy-of select="@*[name() != 'unit']"/>
          <xsl:attribute name="unit">TT2</xsl:attribute>
  </xsl:copy>              
</xsl:template>


Many thanks for any help ? Maybe i need to start again and rethink but my transformations, i think i am perhaps doing things in the wrong order....

Thanks

m
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Hi Molko,

2) must be very similar to 1)
    <xsl:template match="datum[@name='Amount'][parent::data[datum[@attribute='ind'][@value='Yes']]]">
        <xsl:copy>
            <xsl:copy-of select="@*[name() != 'unit']"/>
            <xsl:attribute name="unit">MT2</xsl:attribute>
        </xsl:copy>              
    </xsl:template>


Cheers!
Molko,

and here is 3)

    <xsl:template match="datum[@name='Age']"/>
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of avernet
avernet

Molko,

Gertone stylesheet seems to work just great. BTW, to test it out quickly on your XML, you can use the page below, paste the XML you want to transform in the first text area, paste the XSLT in the second one, and the result appears at the bottom. There is no "submit" button and the result of the transformation is updated as you type.

http://www.orbeon.com/ops/goto-example/xslt

Alex
--
Blog (XML, Web apps, Open Source):
http://www.orbeon.com/blog/

Avatar of Molko

ASKER

Nice one, i have learnt a lot from that...
Molko,

I am flattered that you marked my comment at the accepted answer, but shouldn't Gertone's comments be marked as accepted answer instead? He is the one who really wrote the stylesheet that solves your problem :).

Alex
--
Blog (XML, Web apps, Open Source):
http://www.orbeon.com/blog/

Avatar of Molko

ASKER

avernet

Oooops, sorry that was a slip of the mouse, how can i undo my error ?
Molko,

Try posting a message in the Community Support, asking an admin to do this change for you. Questions in Community Support don't use your points.

https://www.experts-exchange.com/Community_Support/

Alex
--
Blog (XML, Web apps, Open Source):
http://www.orbeon.com/blog/