Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Multiple conditions on child node selection

Posted on 2011-03-06
3
287 Views
Last Modified: 2013-11-18
Hi Experts,

I need some help formulating an XSL template that will output what I need from the attached XML file.

For each <tsfile> I need to locate the last <p> which has <id>[2] = 0.
Then return all <p>'s after the above position()

I've had a go, but I'm still learning XSLT and not having much luck.

Thanks
<?xml version="1.0" standalone="yes"?>
<h_xml_store>
  <tsfile site="A">
    <variable variable="200.00">
      <p>
        <id>1 Jan</id>
        <id>20</id>
      </p>
      <p>
        <id>2 Jan</id>
        <id>0</id>
      </p>
      <p>
        <id>3 Jan</id>
        <id>1</id>
      </p>
      <p>
        <id>4 Jan</id>
        <id>2</id>
      </p>
      <p>
        <id>5 Jan</id>
        <id>0</id>
      </p>
      <p>
        <id>6 Jan</id>
        <id>1</id>
      </p>
    </variable>
  </tsfile>
  <tsfile site="B">
    <variable variable="200.00">
      <p>
        <id>3 March</id>
        <id>14</id>
      </p>
      <p>
        <id>4 March</id>
        <id>0</id>
      </p>
      <p>
        <id>8 March</id>
        <id>1</id>
      </p>
      <p>
        <id>9 March</id>
        <id>2</id>
      </p>
    </variable>
  </tsfile>
</h_xml_store>

Open in new window

0
Comment
Question by:SlobberyBoxerDog
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 250 total points
ID: 35054032
sounds like you need this
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0">
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="variable">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="p[id[2] = 0][not(following-sibling::p[id[2] = 0])]/following-sibling::node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 1

Author Closing Comment

by:SlobberyBoxerDog
ID: 35054924
Thanks Gertone, works perfect.

Can you suggest any good XSLT books for beginners ? I'm a c# coder and I cant get my head out of procedural mode when working on XSLT
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 35054935
this one is a very good learning book
http://my.safaribooksonline.com/book/databases/xml/0596000537

the latest edition also covers XSLT2
http://my.safaribooksonline.com/9780596527211
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question