Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XSL compare and change XML structure

 
E Robb
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been working with XSL a little and can do simple things but this I have no idea where to begin.

Anything that does not match the Option ID in Options I would like removed from PayBill entityid.

Abbreviated XML original Structure:



Code that I would like to achive with XSL transform



Would greatly appreciate a nudge in the right direction
 
Paul Clapham
Sheriff
Pie
Posts: 20719
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When your requirements say "I want the output to look just like the input except..." then your starting point is an identity transformation. Then add templates which do the specific modifications you're interested in.
 
E Robb
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:When your requirements say "I want the output to look just like the input except..." then your starting point is an identity transformation. Then add templates which do the specific modifications you're interested in.


Hi Paul,

I have a template for upper level sortling of dates in the xml which goes something like this. I would like to incorporate the transformation based on comparing two properties but I dont have a clue as to how to incoprorate it.




Can you elaborate more on how I can compare one properties elements to another properties elements and eliminate anything that does match as I mentioned my original post. Heck I would be happy on terms to google to accomplish the structure from my original post.

Thanks
 
Paul Clapham
Sheriff
Pie
Posts: 20719
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, displaying the XSLT code as a concatenation of Java strings is kind of an odd thing to do, but as far as I can see you've already done what I suggested. There's what looks like the identity transformation at the beginning, and then there's a template which does something with elements which look like <Items type="INOUT">. So that's a good start.

Now, your problem is that you want to drop some <Item> elements which satisfy some condition? Then you write another template which matches <Item> elements. If the element satisifes the condition, then write out the element and inside it you do xsl:apply-templates.

And if I were you I would put that XSLT code into its own file, rather than mixing it up with Java code like that. Easier to maintain, especially having to make sure you do the quotes and apostrophes right.
 
E Robb
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Well, displaying the XSLT code as a concatenation of Java strings is kind of an odd thing to do, but as far as I can see you've already done what I suggested. There's what looks like the identity transformation at the beginning, and then there's a template which does something with elements which look like <Items type="INOUT">. So that's a good start.

Now, your problem is that you want to drop some <Item> elements which satisfy some condition? Then you write another template which matches <Item> elements. If the element satisifes the condition, then write out the element and inside it you do xsl:apply-templates.

And if I were you I would put that XSLT code into its own file, rather than mixing it up with Java code like that. Easier to maintain, especially having to make sure you do the quotes and apostrophes right.


Your first paragraph is correct. I should not be doing strings like that. Jeeze I feel like boozo the clown.

OK can do a xsl:template match but how do I delete? "PayBill entityid="142232"" doesnt equal "Option id="142672"" so Parent of PayBill entity <Item entityid="230138"> and its children need to be removed from the xml.

Sorry Im really a newbie to XSL.

 
Paul Clapham
Sheriff
Pie
Posts: 20719
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't delete anything. The identity transformation copies everything, except the nodes which you match in the other templates. In those templates it's up to you to output whatever you need.

So in the existing template, you output a copy of the <Items> element, only with its children sorted by date.

And in your new template, you only output a copy of the <Item> element if it should be in the output document. Otherwise, you don't output anything.

And your remaining question is how to write an XPath expression to determine whether an <Item> element needs to be kept, right? You seem to have a general idea, so see if you can produce a more precise description.
 
g tsuji
Ranch Hand
Posts: 656
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose you've already an identity transformation somewhere. You can add this specific template to do the specific job.
 
E Robb
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brilliant your a life saver! Im so new to xsl I didnt know where to begin! This give me a great overview, works a treat and I can modify it for a more complicated XML structure.

Thank You!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic