Solved

Multiple conditions on child node selection

Posted on 2011-03-06
3
286 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SP to delete duplicates 15 70
JQuery tracking event. 3 65
XSLT Help 12 32
How to programmatically remove CustomUI ribbon XML customisations from MSOffice files using VBA? 9 139
Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now