gpt4 book ai didi

xml - 如何通过XML文档中的XPath查询查找祖先元素?

转载 作者:行者123 更新时间:2023-12-03 16:40:21 24 4
gpt4 key购买 nike

我试图显示J.K. Rowling从下面粘贴的Xml文档中撰写的所有项目的ASIN。我不知道如何编写XPath,因为ASIN在ItemAttributes之外。我知道应该是这样的:
/ ItemSearchResponse / Items / Item / ItemAttributes [Author =“ J。K. Rowling”] /作者
但是我不知道该去哪里。有没有人有什么建议?






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

最佳答案

这是一个仅转发的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>


在提供的(片段,包裹在单个顶部元素中以使其格式正确)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元素的字符串值中空格的不同数量。

关于xml - 如何通过XML文档中的XPath查询查找祖先元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15537928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com