aspose file tools*
The moose likes XML and Related Technologies and the fly likes XSL/Xpath help needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XSL/Xpath help needed" Watch "XSL/Xpath help needed" New topic
Author

XSL/Xpath help needed

Vrinda Werdel
Ranch Hand

Joined: Jan 03, 2004
Posts: 75
Hi all,

I am trying to write an XSL for an XML to output another XML.

The input XML looks like this:



I need to convert this to the following output:


I need to know a couple of things.

1. how can I write xpath that loops through elements of Info/Address
2. How can I select the name of the element from the input xml and represent it as value in the output xml?
3. how can I use string functions to produce <data name="BillingAddressInfo">? [concat address+info]

Any help is appreciated.

regards
Vrinda
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Do you really want this line? It seems not making sense.

<type>BillingAddress</type>
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Does <FirstName><LastName> have value in-between, empty elements do not make sense either.
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
BillingAddress/ShippingAddress are names???
Vrinda Werdel
Ranch Hand

Joined: Jan 03, 2004
Posts: 75
Do you really want this line? It seems not making sense.

<type>BillingAddress</type>


Yes. This is needed as it is part of the requirement.

Does <FirstName><LastName> have value in-between, empty elements do not make sense either.


The <FirstName><LastName> elements may or may not have values.

BillingAddress/ShippingAddress are names???


They are element names. they contain other child elements.

thanks
Vrinda
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Then, how about:

<type>ShippingAddress</type>
Vrinda Werdel
Ranch Hand

Joined: Jan 03, 2004
Posts: 75
that's how the output should look like in the output XML.

Vrinda
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Originally posted by Vrinda Werdel:
1. how can I write xpath that loops through elements of Info/Address
Well, the XPath expression "Info/Address" does give you a list of the elements you want. But it doesn't "loop through" them, it just is a list. Actually you should stop thinking of looping through things in XSLT and XPath, as that will lead you to write ugly code. It's better to accept XSLT for the functional language that it is and not try to write procedural code as if it were strangely-formatted Java. In other words, write templates that transform particular elements in the way you want them transformed.

You should be able to find examples of that style of XSLT programming in any basic book or tutorial about the language.
2. How can I select the name of the element from the input xml and represent it as value in the output xml?
That would be the name() or local-name() XPath function. And you can use an xsl-element element to create an element with a name that you don't know, like this:However in your case you already know the name of the element you want to create, so I would simply write an ordinary XML element with that name.
3. how can I use string functions to produce <data name="BillingAddressInfo">? [concat address+info]
Yes, there's a concat() function in XPath.

But all these questions you are asking suggest to me that you have never read a book or even a tutorial about XSLT. You won't find it easy to learn a programming language by asking questions on a forum. I suggest you should get yourself a reference and start reading.
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Here is an answer based on your question, but not exact.

It will give you some help.

http://bobcat.webappcabaret.net/javachina/faq/xslt_01.htm#xslt_Q11
[ July 24, 2007: Message edited by: Roseanne Zhang ]
 
jQuery in Action, 2nd edition
 
subject: XSL/Xpath help needed