web-dev-qa-db-ja.com

XPath関数 'substring-after'を適用する方法

各本の「HarryPotter:」に続く文字列を取得するために使用するXPath式は何ですか。

すなわち。このXMLを考えると:

<bookstore>
<book>
  HarryPotter:Chamber of Secrets 
</book>
<book>
  HarryPotter:Prisoners in Azkabahn 
</book>
</bookstore>

私は戻ってきます:

Chamber of Secrets
Prisoners in Azkabahn 

私はこのようなことを試しました:

/bookstore/book/text()[substring-after(. , 'HarryPotter:')] 

私の構文は間違っていると思います...

17
Dan

XMLではハリーポッターの間にスペースはありませんが、XQueryではスペースがあります。一致させてプレストするだけで、データが返されます...

    Dim xml = <bookstore>
                  <book>HarryPotter:Chamber of Secrets</book>
                  <book>HarryPotter:Prisoners in Azkabahn</book>
                  <book>MyDummyBook:Dummy Title</book>
              </bookstore>

    Dim xdoc As New Xml.XmlDocument
    xdoc.LoadXml(xml.ToString)

    Dim Nodes = xdoc.SelectNodes("/bookstore/book/text()[substring-after(., 'HarryPotter:')]")

    Dim Iter = Nodes.GetEnumerator()
    While Iter.MoveNext
        With DirectCast(Iter.Current, Xml.XmlNode).Value
            Console.WriteLine(.Substring(.IndexOf(":") + 1))
        End With
    End While
5
BenAlabaster

XPath式

/bookstore/book/text()[substring-after(. , 'HarryPotter:')]

値「HarryPotter:」を含むすべてのテキストノードを含むノードセットを返します。 「HarryPotter:」に続く本のタイトルを取得するには、このノードセットを調べて、ノードごとにその部分文字列をフェッチする必要があります。これは、部分文字列を使用して実行できます-XSLTを使用している場合はafterを使用し、VB)を使用している場合はsubstringとIndexOfを使用します。

4
Johan L

Xpath:

substring-after(//book, 'HarryPotter:')

私はこの分野ではまったく新しいですが、私にとってはうまくいきます...!

0
Zahra Hnn