user2025508
user2025508

Reputation: 71

How to find an ancestor element with an XPath Query from an XML Document?

I am trying to show the ASIN of all the items authored by J. K. Rowling from the Xml document I pasted below. I don't know how to write the XPath since the ASIN is outside of the ItemAttributes. I know it should be something like this: /ItemSearchResponse/Items/Item/ItemAttributes[Author="J. K. Rowling"]/Author but I dont know where to but the ASIN. Does anyone have any suggestions?

<Item>
  <ASIN>0747557462</ASIN>
  <ItemAttributes>
    <Author>Sarah Brown</Author>
    <Author>Gil McNeil</Author>
    <Creator Role="Foreword">J.K. Rowling</Creator>
    <Manufacturer>Bloomsbury UK</Manufacturer>
    <ProductGroup>Book</ProductGroup>
    <Title>Magic</Title>
  </ItemAttributes>
</Item>
<Item>
  <ASIN>0826452329</ASIN>t
  <ItemAttributes>
    <Author>Philip Nel</Author>
    <Manufacturer>Continuum</Manufacturer>
    <ProductGroup>Book</ProductGroup>
    <Title>J.K. Rowling's Harry Potter Novels: A Reader's Guide (Continuum Contemporaries) - Unauthorized</Title>
  </ItemAttributes>
</Item>
<Item>
  <ASIN>1571745424</ASIN>
  <ItemAttributes>
    <Author>George W. Beahm</Author>
    <Creator Role="Illustrator">Tim Kirk</Creator>
    <Manufacturer>Hampton Roads Pub Co</Manufacturer>
    <ProductGroup>Book</ProductGroup>
    <Title>Muggles and Magic: An Unofficial Guide to J.k. Rowling and the Harry Potter Phenomenon</Title>
  </ItemAttributes>
</Item>
<Item>
  <ASIN>0737716681</ASIN>
  <ItemAttributes>
    <Creator Role="Editor">Gary Wiener</Creator>
    <Manufacturer>Greenhaven Press</Manufacturer>
    <ProductGroup>Book</ProductGroup>
    <Title>JK Rowling (hardcover edition) (Literary Companion to Contemporary Authors)</Title>
  </ItemAttributes>
</Item>
<Item>
  <ASIN>1586638394</ASIN>
  <ItemAttributes>
    <Author>SparkNotes Editors</Author>
    <Manufacturer>SparkNotes</Manufacturer>
    <ProductGroup>Book</ProductGroup>
    <Title>J.K. Rowling (SparkNotes Library of Great Authors)</Title>
  </ItemAttributes>
</Item>
<Item>
  <ASIN>0972322108</ASIN>
  <ItemAttributes>
    <Author>John Granger</Author>
    <Manufacturer>Zossima Press</Manufacturer>
    <ProductGroup>Book</ProductGroup>
    <Title>The Hidden Key to Harry Potter: Understanding the Meaning, Genius, and Popularity of Joanne Rowling's Harry Potter Novels</Title>
  </ItemAttributes>
</Item>
<Item>
  <ASIN>1414321880</ASIN>
  <ItemAttributes>
    <Author>John Granger</Author>
    <Manufacturer>SaltRiver</Manufacturer>
    <ProductGroup>Book</ProductGroup>
    <Title>How Harry Cast His Spell: The Meaning Behind the Mania for J. K. Rowling's Bestselling Books</Title>
  </ItemAttributes>
</Item>
<Item>
  <ASIN>0545010225</ASIN>
  <ItemAttributes>
    <Author>J. K. Rowling</Author>
    <Creator Role="Illustrator">Mary GrandPré</Creator>
    <Manufacturer>Arthur A. Levine Books</Manufacturer>
    <ProductGroup>Book</ProductGroup>
    <Title>Harry Potter and the Deathly Hallows (Book 7)</Title>
  </ItemAttributes>
</Item>
<Item>
  <ASIN>B000RBZM6E</ASIN>
  <ItemAttributes>
    <Author>J.K. Rowling</Author>
    <Creator Role="Translator">Gili Bar-Hillel</Creator>
    <Manufacturer>Yediot Acharonot</Manufacturer>
    <ProductGroup>Book</ProductGroup>
    <Title>Harry Potter and the Goblet of Fire (Hebrew) (English and Hebrew Edition)</Title>
  </ItemAttributes>
</Item>
<Item>
  <ASIN>0439705908</ASIN>
  <ItemAttributes>
    <Author>J. K. Rowling</Author>
    <Manufacturer>Scholastic</Manufacturer>
    <ProductGroup>Book</ProductGroup>
    <Title>Harry Potter and the Order of the Phoenix (Year 5)</Title>
  </ItemAttributes>
</Item>

Upvotes: 2

Views: 233

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243479

Here is a forward-only XPath expression that selects exactly the wanted nodes:

/*/*[translate(ItemAttributes/Author, ' ', '')='J.K.Rowling']/ASIN

XSLT - based verification:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:copy-of select=
  "/*/*[translate(ItemAttributes/Author, ' ', '')='J.K.Rowling']/ASIN"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided (fragment, wrapped in a single top element to become wellformed) XML document:

<t>
    <Item>
        <ASIN>0747557462</ASIN>
        <ItemAttributes>
            <Author>Sarah Brown</Author>
            <Author>Gil McNeil</Author>
            <Creator Role="Foreword">J.K. Rowling</Creator>
            <Manufacturer>Bloomsbury UK</Manufacturer>
            <ProductGroup>Book</ProductGroup>
            <Title>Magic</Title>
        </ItemAttributes>
    </Item>
    <Item>
        <ASIN>0826452329</ASIN>t
        <ItemAttributes>
            <Author>Philip Nel</Author>
            <Manufacturer>Continuum</Manufacturer>
            <ProductGroup>Book</ProductGroup>
            <Title>J.K. Rowling's Harry Potter Novels: A Reader's Guide (Continuum Contemporaries) - Unauthorized</Title>
        </ItemAttributes>
    </Item>
    <Item>
        <ASIN>1571745424</ASIN>
        <ItemAttributes>
            <Author>George W. Beahm</Author>
            <Creator Role="Illustrator">Tim Kirk</Creator>
            <Manufacturer>Hampton Roads Pub Co</Manufacturer>
            <ProductGroup>Book</ProductGroup>
            <Title>Muggles and Magic: An Unofficial Guide to J.k. Rowling and the Harry Potter Phenomenon</Title>
        </ItemAttributes>
    </Item>
    <Item>
        <ASIN>0737716681</ASIN>
        <ItemAttributes>
            <Creator Role="Editor">Gary Wiener</Creator>
            <Manufacturer>Greenhaven Press</Manufacturer>
            <ProductGroup>Book</ProductGroup>
            <Title>JK Rowling (hardcover edition) (Literary Companion to Contemporary Authors)</Title>
        </ItemAttributes>
    </Item>
    <Item>
        <ASIN>1586638394</ASIN>
        <ItemAttributes>
            <Author>SparkNotes Editors</Author>
            <Manufacturer>SparkNotes</Manufacturer>
            <ProductGroup>Book</ProductGroup>
            <Title>J.K. Rowling (SparkNotes Library of Great Authors)</Title>
        </ItemAttributes>
    </Item>
    <Item>
        <ASIN>0972322108</ASIN>
        <ItemAttributes>
            <Author>John Granger</Author>
            <Manufacturer>Zossima Press</Manufacturer>
            <ProductGroup>Book</ProductGroup>
            <Title>The Hidden Key to Harry Potter: Understanding the Meaning, Genius, and Popularity of Joanne Rowling's Harry Potter Novels</Title>
        </ItemAttributes>
    </Item>
    <Item>
        <ASIN>1414321880</ASIN>
        <ItemAttributes>
            <Author>John Granger</Author>
            <Manufacturer>SaltRiver</Manufacturer>
            <ProductGroup>Book</ProductGroup>
            <Title>How Harry Cast His Spell: The Meaning Behind the Mania for J. K. Rowling's Bestselling Books</Title>
        </ItemAttributes>
    </Item>
    <Item>
        <ASIN>0545010225</ASIN>
        <ItemAttributes>
            <Author>J. K. Rowling</Author>
            <Creator Role="Illustrator">Mary GrandPré</Creator>
            <Manufacturer>Arthur A. Levine Books</Manufacturer>
            <ProductGroup>Book</ProductGroup>
            <Title>Harry Potter and the Deathly Hallows (Book 7)</Title>
        </ItemAttributes>
    </Item>
    <Item>
        <ASIN>B000RBZM6E</ASIN>
        <ItemAttributes>
            <Author>J.K. Rowling</Author>
            <Creator Role="Translator">Gili Bar-Hillel</Creator>
            <Manufacturer>Yediot Acharonot</Manufacturer>
            <ProductGroup>Book</ProductGroup>
            <Title>Harry Potter and the Goblet of Fire (Hebrew) (English and Hebrew Edition)</Title>
        </ItemAttributes>
    </Item>
    <Item>
        <ASIN>0439705908</ASIN>
        <ItemAttributes>
            <Author>J. K. Rowling</Author>
            <Manufacturer>Scholastic</Manufacturer>
            <ProductGroup>Book</ProductGroup>
            <Title>Harry Potter and the Order of the Phoenix (Year 5)</Title>
        </ItemAttributes>
    </Item>
</t>

the XPath expression is evaluated and the selected nodes are copied to the output:

<ASIN>0545010225</ASIN>
<ASIN>B000RBZM6E</ASIN>
<ASIN>0439705908</ASIN>

Do note: We ignore the different number of spaces in the string value of any Author element, by eliminating any space, before the comparisson.

Upvotes: 1

Bohemian
Bohemian

Reputation: 425073

You can use .. to access the parent Item then go down into the ASIN tag, adding /../ASIN to your current path.

Try this:

/ItemSearchResponse/Items/Item/ItemAttributes[Author="J. K. Rowling"]/../ASIN

Upvotes: 0

Related Questions