2013-03-21 76 views
2

我想从下面粘贴的Xml文档中显示由J. K. Rowling创作的所有项目的ASIN。我不知道如何编写XPath,因为ASIN不在ItemAttributes中。我知道它应该是这样的: /ItemSearchResponse/Items/Item/ItemAttributes [Author =“J. K. Rowling”] /作者 但我不知道在哪里但ASIN。有没有人有什么建议?如何使用XML文档中的XPath查询查找祖先元素?

<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> 

+0

使用'..'得到父母 – kev 2013-03-21 02:18:19

回答

0

您可以使用..来访问父项,然后下到ASIN标签,增加/../ASIN到当前的路径。

试试这个:

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

谢谢你的工作! – user2025508 2013-03-21 04:12:06

1

这里是一个只进XPath表达式选择完全想节点:

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

XSLT - 基于验证

<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> 

当该变换被应用所提供的(片段,包装在单个顶部元件成为简洁(wellformed))的XML文档

<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> 

XPath表达式求值和所选择的节点被复制到输出:

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

待办事项:我们忽略任何Author EL的字符串值的不同数量的空间通过消除任何空间,在比较之前。