Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HELP with XSLT

 
Pinaki Sarkar
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Dmitry Kirsanov
Author
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic