• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

xsl:if and XPath

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hope this helps?
 
damian cosmos
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your help.
The orginal xsl that you sent me did the trick.
Regards,
Damian.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic