• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

HELP with XSLT

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am a beginner in using XSLT. I am having a problem in tranforming an XML element to a fixed position in a message when the position of the corresponding element in the source message is variable.

eg:

XML1:

<personal>
<personalDetails>
<name>String</name>
<age>String</age>
<address>
<address1>String</address1>
<address2>String</address2>
</address>
</personalDetails>
</personal>

XML2:

<personal>
<personalDetails>
<name>String</name>
<age>String</age>
</personalDetails>
<address>
<address1>String</address1>
<address2>String</address2>
</address>
</personal>


The response XML should look like this
<xsl:template match="address">
<ADDRESS>
<xsl:choose>
<xsl:when test="ancestor: ersonal">
<ADDRESS1>
<xsl:value-of select="address1"/>
</ADDRESS1>
<ADDRESS2>
<xsl:value-of select="address1"/>
</ADDRESS2>
</xsl:when>
<xsl therwise>
<ADDRESS1>
<xsl:value-of select="address1"/>
</ADDRESS1>
<ADDRESS2>
<xsl:value-of select="address1"/>
</ADDRESS2>
</xsl therwise>
</xsl:choose>
</ADDRESS>
</xsl:template>

But the problem it just prints the nodes in what format it comes. Please could anyone suggest a way out of this problem.
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch ... now that you're here though ... I have no idea what you're trying to do. How about explaining some more.
 
Pinaki Sarkar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry i missed the response XML

<PERSONAL>
<PERSONALDETAILS>
<NAME>String</NAME>
<AGE>String</AGE>
<ADDRESS>
<ADDRESS1>String</ADDRESS1>
<ADDRESS2>String</ADDRESS2>
</ADDRESS>
</PERSONALDETAILS>
</PERSONAL>

If you see XML1 and XML2, the <address> element and its childs can come either within the <personalDetails> element or within the <personal> element.

But the transformed XML must always have the <ADDRESS> element within the <PERSONALDETAILS> tag.

So the problem i am currently facing is how to manipulate the position of address element.
 
Author
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By default, XSLT implements the "push" paradigm: source constructs are pushed into the stylesheet in the same order as they come in the source. However it is also capable of implementing the "pull" style, where the stylesheet controls what information from the source and in what order is processed. What you need here is a bit of pull XSLT, for example:

<xsl:template match="personal">
<PERSONAL>
<PERSONALDETAILS>
<xsl:apply-templates select=".//NAME"/>
<xsl:apply-templates select=".//AGE"/>
<xsl:apply-templates select=".//ADDRESS"/>
</PERSONALDETAILS>
</PERSONAL>
</xsl:template>

Then you provide separate XSLT templates for the name, age, and address elements, and these templates will get called in the order specified here, no matter what is their order in the source. These templates may be simple xsl:copy if you don't need to do any processing.
 
I need a new interior decorator. This tiny ad just painted every room in my house purple.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic