2

我有以下 XML:

<thoughts>
    <thought>
        <id>1</id>
        <category>Leadership</category>
        <what>sometext</what>            
        <who>sometext</who>
    </thought>
    <thought>
        <id>2</id>
        <category>Leadership</category>
        <what>sometext</what>            
        <who>sometext</who>
    </thought>
    ... 100s of category Leadership
    <thought>
        <id>1</id>
        <category>Love</category>
        <what>sometext</what>            
        <who>sometext</who>
    </thought>
    <thought>
        <id>2</id>
        <category>Love</category>
        <what>sometext</what>            
        <who>sometext</who>
    </thought>
    ... 100s of category Love

    ... and so on up to about ten categories
</thoughts>

我想为给定的 id 和类别选择一个想法(什么)。我正在用 Java 做这个。我尝试了以下方法:

"/thought[id='1']/thought[category='Love']/what/text()"

爪哇:

XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
XPathExpression expr1 = xPath.compile("/thought[id='1']/thought[category='Love']/what/text()");
Object result1 = expr1.evaluate(doc, XPathConstants.NODESET);
NodeList nodes1 = (NodeList) result1;

我也尝试过以下 XPathExpressions:

/thoughts/thought[id='1']/thought[category=`Love`]/what/text()

我是 XML 和 XPath 的新手。

4

2 回答 2

1

使用

/*/thought[id = '1' and category = 'Love']/what

这将选择作为元素what的子元素的任何元素,该thought元素id具有字符串 value 的子"1"元素和字符串categoryvalue的子元素"Love",并且(该thought元素)是 XML 文档顶部元素的子元素。

如果您需要上面选择的元素的文本节点子节点,请使用

/*/thought[id = '1' and category = 'Love']/what/text()

如果您只需要(第一个)上述文本节点的字符串值,请使用

string(/*/thought[id = '1' and category = 'Love']/what)

基于 XSLT 的验证

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

 <xsl:template match="/">
     <xsl:copy-of select="/*/thought[id = '1' and category = 'Love']/what"/>
============
     <xsl:copy-of select="/*/thought[id = '1' and category = 'Love']/what/text()"/>
============
     <xsl:copy-of select="string(/*/thought[id = '1' and category = 'Love']/what)"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档(为所需节点提供的具有不同值的文档)时:

<thoughts>
    <thought>
        <id>1</id>
        <category>Leadership</category>
        <what>sometext</what>
        <who>sometext</who>
    </thought>
    <thought>
        <id>2</id>
        <category>Leadership</category>
        <what>sometext</what>
        <who>sometext</who>
    </thought>
    ... 100's of with category Leadership     
    <thought>
        <id>1</id>
        <category>Love</category>
        <what>some Love text 1</what>
        <who>sometext</who>
    </thought>
    <thought>
        <id>2</id>
        <category>Love</category>
        <what>sometext</what>
        <who>sometext</who>
    </thought>
         ... 100's of with category Love
         ... and so on up to about ten categories 
</thoughts>

对三个 XPath 表达式求值,并将这些求值的结果复制到输出,并用方便的分隔符直观地分隔:

<what>some Love text 1</what>
============
     some Love text 1
============
     some Love text 1

更新

在评论中,OP 添加了不仅应该选择what,而且who应该选择的要求。

下面是这种情况下对应的新 XPath 表达式

/*/thought[id = '1' and category = 'Love']/*[self::what or self::who]

/*/thought[id = '1' and category = 'Love']/*[self::what or self::who]/text()
于 2012-09-15T14:31:58.043 回答
0

我建议遵循 XPath xpression:

/thoughts/thought[id='1' and category='Love']/what/text()

我可以推荐这个教程

于 2012-09-15T13:58:41.980 回答