【问题标题】:XPath expression not functioningXPath 表达式不起作用
【发布时间】:2012-09-08 09:33:25
【问题描述】:

我有以下 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 和类别选择 thought(what)。我正在用 Java 做这个。我尝试了以下方法:

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

Java:

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 的新手。

【问题讨论】:

    标签: java xml dom xpath


    【解决方案1】:

    使用

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

    这会选择任何 what 元素,它是 thought 元素的子元素,具有字符串值为 "1"id 子元素和字符串值为 "Love"category 子元素,以及那(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()
    

    【讨论】:

    • @harshit,此答案中的 XPath 表达式满足您的所有要求。
    【解决方案2】:

    我建议遵循 XPath xpression:

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

    我可以推荐这个tutorial

    【讨论】:

    • 返回NULL。我没有找到太多帮助的教程。请帮忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 2016-06-14
    • 2022-01-14
    相关资源
    最近更新 更多