aspose file tools*
The moose likes XML and Related Technologies and the fly likes XSL compare and change XML structure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XSL compare and change XML structure" Watch "XSL compare and change XML structure" New topic
Author

XSL compare and change XML structure

E Robb
Ranch Hand

Joined: Aug 27, 2010
Posts: 111
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
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

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

Joined: Aug 27, 2010
Posts: 111
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
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

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

Joined: Aug 27, 2010
Posts: 111
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
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

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

Joined: Jan 18, 2011
Posts: 535
    
    3
Suppose you've already an identity transformation somewhere. You can add this specific template to do the specific job.
E Robb
Ranch Hand

Joined: Aug 27, 2010
Posts: 111

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!
 
 
subject: XSL compare and change XML structure