• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Replacing an element value in xml using xsl

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the conversion i only want to change an element value based on a condition not change anything else.

Here is sample xml:
<MsgCourtLinkTxn msgOrigTimeStamp="2008-12-12T11:14:44.828+11:00" msgPayloadID="14194" xmlns="http://www.agd.nsw.gov.au/xml_courtlink/fms">
<MsgRequestData msgReqId="14351" xmlns="">
<fms:Dishonour xmlns:fms="http://www.agd.nsw.gov.au/xml_courtlink/fms">
<fms:TxnDate>2008-12-12+11:00</fms:TxnDate>
<fms:TxnRef>16640</fms:TxnRef>
<fms:LocationContext>229</fms:LocationContext>
<DateDishonour>2008-12-12+11:00</DateDishonour>
<fms:Narrative>REC58067 (redacted) </fms:Narrative>
<fms:BankDishonour>
<fms:GLAccntBankAccnt>1014</fms:GLAccntBankAccnt>
<fms:ChqNumber>691564</fms:ChqNumber>
<fms:BankAmt>-1250.00</fms:BankAmt>
</fms:BankDishonour>
<fms:Appropriation>
<fms:AppType Type="RV"/>
<fms:GLAccntApp>4754</fms:GLAccntApp>
<fms:AppAmt>275.00</fms:AppAmt>
<fms:CostCentre>242</fms:CostCentre>
<fms:Location>246</fms:Location>
<fms:AppTxnRef>0</fms:AppTxnRef>
<fms:CFType Type=" "/>
<fms:RoundType Type=" "/>
<fms:GSTInd Ind="N"/>
</fms:Appropriation>

So in the element that matches fms:Appropriation, for every child element fms:GLAccntApp that equals 4754, change the value of fms:CostCentre to 230.

Any suggestions, have been playing around in xml spy but cant get it to work.

Thanks,
Shane.
XMLimage.jpg
[Thumbnail for XMLimage.jpg]
xml sample
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shane Lee wrote:So in the element that matches fms:Appropriation, for every child element fms:GLAccntApp that equals 4754, change the value of fms:CostCentre to 230.


In other words, for every fms:CostCentre which has a sibling fms:GLAccntApp element whose value is 4754 and whose parent is an fms:Appropriation element, change the value to 230.

When you have words like "which" and "whose" in your problem description, then you're going to use predicates in your match. Something like this, maybe:
 
Shane Lee
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply paul. Ive been working on this for a day and still cant get it to work. Below is my xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fms="http://www.agd.nsw.gov.au/xml_courtlink/fms">
<xsl:output method = "xml" indent="yes" />


<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>


<xsl:template match="fms:CostCentre[../fms:GLAccntApp = '4754' and local-name(..)='fms:Appropriation']">
<xsl:element name="fms:CostCentre">
<xsl:value-of select="'230'"/>
</xsl:element>

</xsl:template>


</xsl:stylesheet>


When you match that predicate, i am trying to output back fms:CostCentre with the value 230, but the ouput does not amend at all it stays the same. I need help please......

<?xml version="1.0" encoding="UTF-8"?>
<MsgCourtLinkTxn xmlns="http://www.agd.nsw.gov.au/xml_courtlink/fms" msgOrigTimeStamp="2008-12-12T11:14:44.828+11:00" msgPayloadID="14194">
<MsgRequestData xmlns="" msgReqId="14351">
<fms:Dishonour xmlns:fms="http://www.agd.nsw.gov.au/xml_courtlink/fms">
<fms:TxnDate>2008-12-12+11:00</fms:TxnDate>
<fms:TxnRef>16640</fms:TxnRef>
<fms:LocationContext>229</fms:LocationContext>
<DateDishonour>2008-12-12+11:00</DateDishonour>
<fms:Narrative>REC58067 (redacted) </fms:Narrative>
<fms:BankDishonour>
<fms:GLAccntBankAccnt>1014</fms:GLAccntBankAccnt>
<fms:ChqNumber>691564</fms:ChqNumber>
<fms:BankAmt>-1250.00</fms:BankAmt>
</fms:BankDishonour>
<fms:Appropriation>
<fms:AppType Type="RV"/>
<fms:GLAccntApp>4754</fms:GLAccntApp>
<fms:AppAmt>275.00</fms:AppAmt>
<fms:CostCentre>242</fms:CostCentre> <fms:Location>246</fms:Location>
<fms:AppTxnRef>0</fms:AppTxnRef>
<fms:CFType Type=" "/>
<fms:RoundType Type=" "/>
<fms:GSTInd Ind="N"/>
</fms:Appropriation>
<fms:Appropriation>
<fms:AppType Type="RV"/>
<fms:GLAccntApp>4136</fms:GLAccntApp>
<fms:AppAmt>100.00</fms:AppAmt>
<fms:CostCentre>242</fms:CostCentre>
<fms:Location>246</fms:Location>
<fms:AppTxnRef>0</fms:AppTxnRef>
<fms:CFType Type=" "/>
<fms:RoundType Type=" "/>
<fms:GSTInd Ind="N"/>
</fms:Appropriation>
<fms:Appropriation>
<fms:AppType Type="RV"/>
<fms:GLAccntApp>4137</fms:GLAccntApp>
<fms:AppAmt>795.45</fms:AppAmt>
<fms:CostCentre>242</fms:CostCentre>
<fms:Location>246</fms:Location>
<fms:AppTxnRef>0</fms:AppTxnRef>
<fms:CFType Type=" "/>
<fms:RoundType Type=" "/>
<fms:GSTInd Ind="T"/>
</fms:Appropriation>
<fms:Appropriation>
<fms:AppType Type="RV"/>
<fms:GLAccntApp>2170</fms:GLAccntApp>
<fms:AppAmt>79.55</fms:AppAmt>
<fms:CostCentre>242</fms:CostCentre>
<fms:Location>246</fms:Location>
<fms:AppTxnRef>0</fms:AppTxnRef>
<fms:CFType Type=" "/>
<fms:RoundType Type=" "/>
<fms:GSTInd Ind="T"/>
</fms:Appropriation>
</fms:Dishonour>
</MsgRequestData>
</MsgCourtLinkTxn>


(I edited your post to hide something that might be confidential personal information and to disable smilies - PC)
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That would suggest that that template doesn't match anything, then. Have you debugged to see if it is ever called?
 
Shane Lee
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,
Yea i debugged and that match template does not match anything, it is stepping over and then just outputting the xml as a whole again. Any ideas!?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then change it so that it does match something. Try something a lot simpler so you can see how matching works, then add in variations of the more complicated parts until you get it working. Don't worry if your interim versions don't work right, as long as they are doing something then you are making progress.

(Note: that's a general description of debugging. It works in all computer languages, not just XSLT.)
 
Shane Lee
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks paul i got it working, but i have another issue now, dealing with namespaces which is a headache in itself. My xml message im transforming has two distinct types, same underlying format, as in both have the underlying parent element fms:Appropriation and child elements fms:GLAccntApp, fms:CostCentre but the annoying thing is that they have different namespace uris:

xmlns:fms="http://www.agd.nsw.gov.au/xml_cicero/fms
xmlns:fms="http://www.agd.nsw.gov.au/xml_courtlink/fms


So in my xsl declaration i can only declare one namespace uri and then match to my fms elements. So it means that i cannot transform one type. Looked into ignoring namespaces and using local names but cant get that to work.

Any ideas? Sorry for the questions, transformation is a bit tricker than the transform im used to doing.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not a problem. Peculiar design, maybe, but not a problem. (And yes, namespaces are a pain.) It's the namespace URI which is important. The prefix you assign to a particular namespace URI in your transformation doesn't have to be the same as the prefix used in the document you are transforming.

So in your transformation you could declare them like this:

And then to identify elements in the first namespace you would use "ci:whatsit", and elements in the second namespace would be "co:whosit".
 
Shane Lee
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks paul i actually did that and had to output the xsl element again but this time with a namepace and it worked for all xml records.

Thanks again for your help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic