aspose file tools*
The moose likes XML and Related Technologies and the fly likes xsl:if and XPath 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 "xsl:if and XPath" Watch "xsl:if and XPath" New topic
Author

xsl:if and XPath

damian cosmos
Ranch Hand

Joined: Dec 13, 2001
Posts: 32
Hi all,
I am new to xml/xsl/xpath and I have been experimenting with the following piece of sample xml
<a>
<b>
<flag>true</flag>
</b>
<c>
<data1>hello</data1>
<data2>goodbye</data2>
</c>
<a>
I am trying to create a stylesheet to process the above xml file.
The flag element can either be true or false.
If true I only want to process the data1 element in element c.
If false I only want to process the data2 element in element c.
I am trying to use and xsl:if as follows
<xsl:if test="//b/flag=true()">
but this gives the following error:
xsl:if requires attribute: test
Any help would be greatly appreciated.
Regards,
Damian.
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
hope this helps?
damian cosmos
Ranch Hand

Joined: Dec 13, 2001
Posts: 32
Thanks karthik.
There is only one problem.
Sometimes my xml can occur in the following format:
<a>
<b>
<flag>true</flag>
</b>
<c>
<data1>hello1</data1>
<data2>goodbye1</data2>
</c>
<c>
<data1>hello2</data1>
<data2>goodbye2</data2>
</c>
<c>
<data1>hello3</data1>
<data2>goodbye3</data2>
</c>
<a>
The content of the first c, data1, data2 sub elements doesn't seem to get displayed. ie hello1 and goodbye1 don't get displayed when using the xsl.
Thanks for all your help.
Regards,
Damian.
damian cosmos
Ranch Hand

Joined: Dec 13, 2001
Posts: 32
Sorry karthik,
I forgot to mention in my last reply that I'm using a for loop to process the c elements.
i.e.
<xsl:for select="following-sibling::c">
and it skips the first data1 data2 elements.
I hope this makes my problem clear to you.
Thanks in advance.
Regards,
Damian.
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
hi Damian,
Originally posted by damian cosmos:

The content of the first c, data1, data2 sub elements doesn't seem to get displayed. ie hello1 and goodbye1 don't get displayed when using the xsl.

Am not so sure as to what you meant when you said you are using a for loop. Anyway the template i posted earlier , it does pick up the first c element's data1 and data2. I use saxon xalan-j2 and saxon6.4.3.
Am not sure if you are looking for a loop like this..
damian cosmos
Ranch Hand

Joined: Dec 13, 2001
Posts: 32
Thanks for all your help.
The orginal xsl that you sent me did the trick.
Regards,
Damian.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: xsl:if and XPath