• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

xsl:if and XPath

 
damian cosmos
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hope this helps?
 
damian cosmos
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your help.
The orginal xsl that you sent me did the trick.
Regards,
Damian.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic