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()"

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は初めてです。

4

2 に答える 2

1

使用

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

これwhatにより、thought要素の子でありid、文字列値を持つ子があり、文字列値"1"を持つcategory子が"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>

3つの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

XPathxpressionに従うことをお勧めします。

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

このチュートリアルをお勧めします

于 2012-09-15T13:58:41.980 に答える