• 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

XSL/Xpath help needed

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you really want this line? It seems not making sense.

<type>BillingAddress</type>
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does <FirstName><LastName> have value in-between, empty elements do not make sense either.
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BillingAddress/ShippingAddress are names???
 
Vrinda Werdel
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then, how about:

<type>ShippingAddress</type>
 
Vrinda Werdel
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's how the output should look like in the output XML.

Vrinda
 
Marshal
Posts: 28177
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

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
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic