30240

How to select the first preceding-sibling which has a particular node as child?

I have been trying to write a XPath statement which will select the first preceding-sibling of a node. That sibling should have a particular node as its child.

For example:

<a> <c></c> .... <b></b> </a> <a></a> .... .... <a></a> <a> <b> start </b> </a>

In the above XML, if my context is at the node a which has start text inside its child b. How can I select the preceding node a whose child is b?

I tried preceding-sibling::*[b][1] but it only selects the node a if b is its first child. I also tried preceding-sibling::*[1][b] but it only checks the first preceding a node and if it doesn't contains b, the test fails.

<strong>Note:</strong> I am using XSLT 1.0

Can anyone suggest me a solution?

Thnx in advance!!

Answer1:

I believe that:

preceding-sibling::*[b][1]

or preferably:

preceding-sibling::a[b][1]

should work just fine.

I tried preceding-sibling::*[b][1] but it only selects the node a if b is its first child.

I don't think that is so.

Answer2:

Like this. ("The first preceding sibling who has a <b> child"):

preceding-sibling::a[b][1]

or this ("The first preceding sibling but only if it has a <b> child"):

preceding-sibling::a[1][b] <hr>

Multiple predicates must be true one after another. Therefore, this:

preceding-sibling::a[b and position() = 1]

is equivalent to the second expression, but not to the first.

Answer3:

With this input:

<root> <a> <c></c> <b>SUCCESS</b> </a> <a>FAIL</a> <a>FAIL</a> <a> <b> start </b> </a> </root>

and this stylesheet

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" /> <xsl:strip-space elements="*"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="a[b[.=' start ']]"> <xsl:copy> <xsl:value-of select="preceding-sibling::a[b]"/> </xsl:copy> </xsl:template> </xsl:stylesheet>

it outputs

<root> <a> <c/> <b>SUCCESS</b> </a> <a>FAIL</a> <a>FAIL</a> <a>SUCCESS</a> </root>

Recommend

  • How to implement xsl
  • Merging two XPathDocuments using XmlCompiledTransform
  • Check all commands exit code within a bash script
  • How to count amount of elements in a row of a matrix in C
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • XSLT foreach repeating nodes to flat
  • Does Apple allow the usage of sysctl.h within iOS applications?
  • Redshift Querying: error xx000 disk full redshift
  • npm 5.4.1 install/uninstall all failing
  • How to get current document uri in XSLT?
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Rails Find when some params will be blank
  • Bad request using file_get_contents for PUT request in PHP
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • Python urlparse: small issue
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Display issues when we change from one jquery mobile page to another in firefox
  • Android screen density dpi vs ppi
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Deselecting radio buttons while keeping the View Model in synch
  • Bug in WPF DataGrid
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Excel - Autoshape get it's name from cell (value)
  • Check if a string to interpolate provides expected placeholders
  • SSO with signing and signature validation doesn't work
  • Validaiting emails with Net.Mail MailAddress
  • Running a C# exe file
  • Deserializing XML into class C#
  • JSON with duplicate key names losing information when parsed
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Javascript + PHP Encryption with pidCrypt
  • How can I estimate amount of memory left with calling System.gc()?
  • Jquery - Jquery Wysiwyg return html as a string
  • RestKit - RKRequestDelegate does not exist
  • Traverse Array and Display in markup
  • Hits per day in Google Big Query
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • costura.fody for a dll that references another dll
  • java string with new operator and a literal