2017-12-06 3 views
0

Ich habe die folgende XML:XPath enthalten genaue Wort

<book> 
    <title>Sword of Honour</title> 
    <category>foo</category> 
    <author>Evelyn Waugh</author> 
</book> 
<book> 
    <title>Moby Dick</title> 
    <category>foo bar</category> 
    <author>Herman Melville</author> 
</book> 
<book> 
    <title>Sayings of the Century</title> 
    <category>bar foo</category> 
    <author>Nigel Rees</author> 
</book> 
<book> 
    <title>The Lord of the Rings</title> 
    <category>bar</category> 
    <author>J. R. R. Tolkien</author> 
</book> 
<book> 
    <title>The Lord</title> 
    <category>foo,bar</category> 
    <author>J. Tolkien</author> 
</book> 

Ich möchte Daten bekommen, wo Kategorie genau "foo" enthält, das wäre:

<book> 
    <title>The Lord</title> 
    <category>foo,bar</category> 
    <author>J. Tolkien</author> 
</book> 
<book> 
    <title>Sword of Honour</title> 
    <category>foo</category> 
    <author>Evelyn Waugh</author> 
</book> 

, wenn ich versuche:

//*[contains(category, "foo")] 

Es gibt die folgenden Elemente zurück:

<book> 
    <title>Sword of Honour</title> 
    <category>foo</category> 
    <author>Evelyn Waugh</author> 
</book> 
<book> 
    <title>Moby Dick</title> 
    <category>foo bar</category> 
    <author>Herman Melville</author> 
</book> 
<book> 
    <title>Sayings of the Century</title> 
    <category>bar foo</category> 
    <author>Nigel Rees</author> 
</book> 
<book> 
    <title>The Lord</title> 
    <category>foo,bar</category> 
    <author>J. Tolkien</author> 
</book> 

während, wenn ich versuche:

//*[category="foo"] 

es gibt die folgenden Elemente:

<book> 
    <title>Sword of Honour</title> 
    <category>foo</category> 
    <author>Evelyn Waugh</author> 
</book> 

Gibt es eine Möglichkeit, dass ich meine Daten bekommen konnte. Jede Hilfe wäre willkommen. Dank

+1

'// * [Kategorie =" Foo "]' gibt 2 Bücher für mich – splash58

+0

@ splash58 In Wirklichkeit hatte ich JSON, die mehrere Kategorien mit Kommas getrennt hat. Aber ich konnte keine ähnliche Frage finden. Also habe ich es in XML konvertiert. Würde es funktionieren, wenn ich den zuletzt mit Komma getrennten konvertiere. Bitte sehen Sie sich die aktualisierte Frage an. – hashtagerrors

+0

Ist eine Kategorieliste mit Leerzeichen nach Komma? – splash58

Antwort

0

Wenn Kategorie Liste des Artikels durch Komma getrennt enthalten:

//category[contains(concat(",", ., ","), ",foo,")]/ancestor::book[1] 

Komma Hinzufügen vor und nach F. E. zu vermeiden benötigt wird, die Auswahl thefoos Kategorie beim Finden foo

+0

Dies gibt einen Fehler. Um genau zu sein, habe ich eine andere Frage gestellt https://Stackoverflow.com/q/47766348/6755753. Es wäre schön, wenn du da helfen könntest. Ich würde dann diese Frage schließen – hashtagerrors

Verwandte Themen