File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes HELP with XSLT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "HELP with XSLT" Watch "HELP with XSLT" New topic
Author

HELP with XSLT

Pinaki Sarkar
Greenhorn

Joined: Jul 01, 2004
Posts: 2
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

Joined: May 29, 2003
Posts: 1258
Welcome to JavaRanch ... now that you're here though ... I have no idea what you're trying to do. How about explaining some more.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Pinaki Sarkar
Greenhorn

Joined: Jul 01, 2004
Posts: 2
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

Joined: Apr 26, 2004
Posts: 33
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: HELP with XSLT
 
Similar Threads
Q: XML to CSV
xsl - differentiating nodes
problem in template matching
Handling Nested tables in xsl:fo for saving a document in RTF
If not null - need help with rule.....