• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

xslt conditionally displaying node elements

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have xml output like this:

<parent>
<child1>
<sib1>
<sib2>
<sib3>
...
<child2>

and want to have <sib1> displaying as a row above another row containing the rest of the sibs only when it changes. i.e. the row containing child1/sib1 will display only once above the data contained in <child1> - <child5>, and then display again above <child6> when the value in <sib1> changes, rather than repeating for each <child>

Is this possible? I've already discovered that declaring the data as a variable won't work, is there some way to compare the value-of the current <sib1> to the previous <sib1> and conditionally display the row?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure. There's <xsl:if> and <xsl:choose> for doing things conditionally. And there's the previous-sibling:: axis for working with previous siblings. Is that enough information? It's hard to tell what you have done so far and what you do and don't know.
 
karyn rudnick
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the prob I'm having with the <xsl:if> is how to format the condition, telling it to look at <sib1> from the previous <child> element and only print the row if it is different. As for the previous::sibling axis, maybe I was having trouble understanding what exactly it is and how to format that. I am pretty new to this so clearly my ignorance is showing here.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't said what your context node is when you're trying to do this comparison. So I'm going to assume it's a <child> element. I'm also going to assume all your <child> elements actually have the same name, instead of all having different names. (If they do all have different names then the designer should be persuaded not to do that.) So your XPath expression would be something like this:And here is an article on the various axes available.
 
karyn rudnick
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, the problem with learning this stuff is that by the time you learn how to use the words properly you don't need as much help anymore.

I ended up using <xsl:value-of select="preceding-sibling::child1[1]/sib1"/> as a variable to compare against a second variable <xsl:value-of select="sib1"/> in an <xsl:if> to determine whether or not to print the data row in the table output. Worked great. Thanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic