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 XSLT Help 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 "XSLT Help" Watch "XSLT Help" New topic
Author

XSLT Help

Hari Shankar Gentyala
Greenhorn

Joined: Sep 23, 2003
Posts: 17
Hi,
I have the following XML file.

<root>
<PDAT></PDAT>
<INS-S DATE="20011023" ID="INS-S-00001"/>
CROSS-
<PDAT>REFERENCE TO RELATED APPLICATION </PDAT>
<INS-E/>
<ITALIC>
<PDAT>
asldfsaklalsasdflkjsadf
</PDAT>
</ITALIC>
</root>

This is just an example of my requirement.My output should be something of this form.
My output should display a special font like <font color="blue> when the text is in between the elements <INS-S/> and <INS-E/>.
The problem is I am unable to track the elements which may come in between these two elements.

Can someone help me out with this problem.

Thanks in advance,
Hari
[ October 17, 2003: Message edited by: Hari Shankar Gentyala ]

G.H.S.Prasad
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Can you explain what are you currently doing so that we might be able to point out where you're going wrong?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
I have got this working, but very awkwardly. Hope it can help you start on it.
<xsl:stylesheet version = "1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="alltextnodes" select="/root/text() | /root/*"/>
<xsl:for-each select="$alltextnodes">
<xsl:if test="local-name() = 'INS-S'">
<xsl:variable name="start" select="position()"/>
<xsl:for-each select="$alltextnodes">
<xsl:if test="local-name() = 'INS-E'">
<xsl:variable name="end" select="position()"/>
<xsl:for-each select="$alltextnodes">
<xsl:if test="position() > $start and position() < $end ">
<font color="red"><xsl:value-of select="."/></font>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>


Tony Yan<br /> <br />IBM Certified Developer XML and Related Technology<br />Sun Certified Web Component Developer For J2EE Platform<br />Sun Certified Programmer For Java 2 Platform
Hari Shankar Gentyala
Greenhorn

Joined: Sep 23, 2003
Posts: 17
Thanks Tony for your help,
Here is my actual XML file.
<RELAPP>
<BTEXT>
<H LVL="1">
<STEXT>
<PDAT>
<INS-S DATE="20011023" ID="INS-S-00001"/>
CROSS-
</PDAT>
<HIL>
<ITALIC>
<PDAT>REFERENCE TO RELATED APPLICATION
<INS-E ID="INS-S-00001"/>
</PDAT>
</ITALIC>
</HIL>
</STEXT>
</H>
<PARA ID="P-00002" LVL="0">
<PTEXT>
<PDAT>This
<INS-S DATE="20011023" ID="INS-S-00002"/>
is a continuation of application Ser. No.
</PDAT>
<HIL>
<BOLD>
<PDAT>08</PDAT>
</BOLD>
</HIL>
<PDAT>, now abandoned, which
<INS-E ID="INS-S-00002"/>
is a continuation of co-pending application Ser. No. 07/160,630 filed on 26 Feb. 1988 now abandoned.
</PDAT>
</PTEXT>
</PARA>
</BTEXT>
</RELAPP>

And the output should be something in this form:
<font color="red">
CROSS-REFERENCE TO RELATED APPLICATION</font>This <font color="red">is a continuation of application Ser. No.08, now abandoned, which </font>
is a continuation of co-pending application Ser. No. 07/160,630 filed on 26 Feb. 1988 now abandoned.
i.e after the element INS-S starts, the font should change to red and this font should end with the element INS-E.The remaining text should display same.

Can you help me with this requirement.
thanks,
Hari
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
In that case, it gets simpler like this.

Here is the output from my transformer(xalan):
<font color="red"> CROSS- REFERENCE TO RELATED APPLICATION </font> This <font color="red"> is a continuation of application Ser. No. 08, now abandoned, which </font> is a continuation of co-pending application Ser. No. 07/160,630 filed on 26 Feb. 1988 now abandoned.

Regards,
Hari Shankar Gentyala
Greenhorn

Joined: Sep 23, 2003
Posts: 17
Hi Tony,
Thanks for your reply but my XSLT processor(Xalan) is reporting error as this is not a well formed document.

It is saying as if the <font> is to be closed.

Thanks,
Hari
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
You can encapsulate the <font> element into a <![CDATA[...]]> block.
EDIT: I noticed a syntax error and corrected it
[ October 23, 2003: Message edited by: Lasse Koskela ]
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
Lasse is right. Or, you can use entities like & l t ; (remove the space) to replace <
Rgds,
Hari Shankar Gentyala
Greenhorn

Joined: Sep 23, 2003
Posts: 17
Thanks Tony and Lasse,
I tried that way.It is printing <font > but not changing the color of my application.
Can you help me.

Thanks,
Hari
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
Pls show the result you got, i.e. the HTML code.
Regards,
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Are you using something like
Because if you are, you should remove the entities and just use(the parser won't complain now because it doesn't try to parse the contents of a CDATA block.
[ October 23, 2003: Message edited by: Lasse Koskela ]
Hari Shankar Gentyala
Greenhorn

Joined: Sep 23, 2003
Posts: 17
Hi Lasse and Tony,
I am using <![CDATA[<font color="red">]]> but my HTML output is not changing the font to red but instead it displays this output.

<font color="red"> CROSS- REFERENCE TO RELATED APPLICATION </font> This <font color="red"> is a continuation of application Ser. No. 08/152,253, filed on Nov. 12, 1993, now abandoned; which is a continuation of Ser. No. 07/864,492 filed on Apr. 7, 1992, now abandoned, which </font> is a continuation of co-pending application Ser. No. 07/160,630 filed on 26 Feb. 1988 now abandoned.
Let me know if i need to modify any code.
Thanks,
Hari
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Try adding <xslutput method="text"/> to the beginning of the stylesheet document.
[ October 23, 2003: Message edited by: Lasse Koskela ]
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
The HTML code looks good, it presents correct color in my IE.
Just a reminder, don't use IE's internal xslt transformer to display your xml with the xsl as href. In that case, the html markup is disabled so you will see the HTML code as it is. So <font color="red">xxxx</font> won't be marked up as red letters. Instead, text as they are will be displayed. Use something like xalan or xt to transform and pipe the code to a browser.
Ignore if it doesn't apply. In any events, html code is good now. problem is within the browser markup step.
Regards
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
The HTML code looks good, it presents correct color in my IE.
Just a reminder, don't use IE's internal xslt transformer to display your xml with the xsl as href. In that case, the html markup is disabled so you will see the HTML code as it is. So <font color="red">xxxx</font> won't be marked up as red letters. Instead, text as they are will be displayed. Use something like xalan or xt to transform and pipe the code to a browser.
Ignore if it doesn't apply. In any events, html code is good now. problem is within the browser markup step.
Regards
Hari Shankar Gentyala
Greenhorn

Joined: Sep 23, 2003
Posts: 17
Thanks Lasse and Tony,
It is working fine now.
Thanks again
Hari
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XSLT Help