cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Quick Xpath question

Highlighted
Regular Member

Quick Xpath question

<figure><title></title>
<subfig><graphic></graphic><subfig>
<subfig><graphic></graphic><subfig>
<subfig><graphic></graphic><subfig>
</figure>


If I am in subfig #2, and I want to access an attribute (graphsty) of the graphic in subfig #1, would this do? (I am only interested in the graphic immediately preceding the subfig I am in.)


preceding-sibling::subfig[1]/graphic[1][@graphsty] or
preceding-sibling[1]/graphic[1][@graphsty] ?


and if I was in a graphic I would access the previous graphic by


../preceding-sibling::subfig[1]/graphic[1][@graphsty]?

Tags (2)
7 REPLIES 7
Highlighted

Quick Xpath question

Hi, Caroline...

Your first version should be just about right. The second would not work
because the name of an axis, such as "preceding-sibling", must always be
followed by two colons and a node test, such as an element name or wildcard
(*). Without that, XPath will instead look for elements named
"preceding-sibling" as children of the current element.

Keep in mind, also, that expressions in square brackets are just filters,
rather than steps in the path, so the "[@graphsty]" at the end will just
discard the "graphic" element if it doesn't have a "graphsty" attribute,
rather than causing the expression to return the attribute, if present.
So, you'll probably want "/@graphsty" at the end, instead.

-Brandon 🙂


On Tue, Jul 30, 2013 at 11:47 PM, Caroline Leccese <
caroline@thecodesource.net> wrote:

> <figure><title></title>
> <subfig><graphic></graphic><subfig>
> <subfig><graphic></graphic><subfig>
> <subfig><graphic></graphic><subfig>
> </figure>
>
> If I am in subfig #2, and I want to access an attribute (graphsty) of the
> graphic in subfig #1, would this do? (I am only interested in the graphic
> immediately preceding the subfig I am in.)
>
> preceding-sibling::subfig[1]/graphic[1][@graphsty] or
> preceding-sibling[1]/graphic[1][@graphsty] ?
>
> and if I was in a graphic I would access the previous graphic by
>
> ../preceding-sibling::subfig[1]/graphic[1][@graphsty]?
>
Highlighted

RE: Quick Xpath question

Thanks Brandon!



So this is what they should be?



preceding-sibling::subfig[1]/graphic[1][/@graphsty] from <subfig>


../preceding-sibling::subfig[1]/graphic[1][/@graphsty] from <graphic>

Highlighted

RE: Quick Xpath question

From graphic in subfig, I want to check the graphsty att of the graphic in the preceding subfig. I test full-sheet.yn in subfig, but my results are off. The first time the variable should switch from y to n or vice versa there's a delay in my results. Is my logic wrong? If I do ../preceding-sibling::subfig[1]/graphic[1][/@graphsty] I only get 'y' values.


<graphic in=" subfig="> (graphsty=1 means a half-sheet graphic)


<att>
<specval attname="../preceding-sibling::subfig[1]/graphic[1][@graphsty]"&lt;br"/>attloc="#xpath" attval="1"/>
<charsubset>
<savetext textid="full-sheet.yn" conrule="\n"/">
</charsubset>
</att>


<att>
<specval attname="../preceding-sibling::subfig[1]/graphic[1][@graphsty]"&lt;br"/>attloc="#xpath" attval="0"/>
<charsubset>
<savetext textid="full-sheet.yn" conrule="\y"/"></charsubset>
</att>


test results from <subfig>:
<graphic>full-sheet=y (correct but irrelevant)


<graphic>full-sheet=y (correct)


<graphic graphsty="1">full-sheet=y (correct)


<graphic graphsty="1">full-sheet=n (incorrect)
<graphic graphsty="1">full-sheet=n (correct)


<graphic>full-sheet=n (correct)


<graphic>full-sheet=n (incorrect)


<graphic>full-sheet=y (correct)


<graphic>full-sheet=y (correct)

RE: Quick Xpath question

Ahh, never mind. preceding-sibling::subfig[1]/graphic[1][@graphsty] works if I set full-sheet.yn in <subfig> instead of <graphic> in <subfig>.

Highlighted

RE: Quick Xpath question

Arg. If I'm in a following-sibling of figure and I want to get to the preceding graphic, which could be in a subfig or figure, would this do?
preceding-sibling::*/graphic[1][@graphsty] or


preceding-sibling::*[1]/graphic[1][@graphsty]


I don't think that's right.

Highlighted

Quick Xpath question

In questions of XPath, I find it always best to break it down to steps:

preceding-sibling::* = get me a lit of every tag that is at the same level as me, looking "backwards" towards the start of the document (but don't leave our parent tag when looking)
preceding-sibling::*[1] = get the tag immediately before me (as long as it is still inside our parent tag)

whatever/graphic[1] = from the list of <graphic> tags under <whatever> get me the first one.

whatever[@graphsty] = filter the list of <whatever> picking only those which have a graphsty attribute set on them.

I think what you need in this case is a bit different from your example XPath. Assuming your context is a <figure> and you want the first <graphic> of a <figure> before you (where the <graphic> may be nested in a <subfig>) then you want:
Highlighted

RE: Quick Xpath question

Thank you, Gareth!

Announcements