Solved

Multiple conditions on child node selection

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ColdFusion Rereplace 3 71
Microsoft Edge 9 91
React or Angular? 6 52
msxml3.dll error '80072efd' A connection with the server could be not established 8 30
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

911 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

20 Experts available now in Live!

Get 1:1 Help Now