xml 如何应用 XPath 函数“substring-after”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/333249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:14:13  来源:igfitidea点击:

How to apply the XPath function 'substring-after'

xmlxpath

提问by Dimitre Novatchev

What is the XPath expression that I would use to get the string following 'HarryPotter:' for each book.

我将使用什么 XPath 表达式来获取每本书的 'HarryPotter:' 后面的字符串。

ie. Given this XML:

IE。鉴于此 XML:

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

I would get back:

我会回来:

Chamber of Secrets
Prisoners in Azkabahn 

I have tried something like this:

我试过这样的事情:

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

I think my syntax is incorrect...

我认为我的语法不正确...

回答by Dimitre Novatchev

In XPath 2.0 this can be produced by a single XPath expression:

在 XPath 2.0 中,这可以由单个 XPath 表达式生成:

      /*/*/substring-after(., 'HarryPotter:')

      /*/*/substring-after(., 'HarryPotter:')

Here we are using the very powerful feature of XPath 2.0 that at the end of a path of location steps we can put a function and this function will be applied on all nodes in the current result set.

这里我们使用了 XPath 2.0 非常强大的功能,在位置步骤路径的末尾,我们可以放置一个函数,该函数将应用于当前结果集中的所有节点。

In XPath 1.0 there is no such featureand this cannot be accomplished in one XPath expression.

在 XPath 1.0 中没有这样的特性,这不能在一个 XPath 表达式中完成。

We could perform an XSLT transformation like the following:

我们可以执行如下的 XSLT 转换:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

    <xsl:template match="/">
      <xsl:for-each select="/*/*[substring-after(., 'HarryPotter:')]">
        <xsl:value-of select=
         "substring-after(., 'HarryPotter:')"/>
        <xsl:text>&#xA;</xsl:text>
      </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>  

When applied on the original XML document:

应用于原始 XML 文档时:

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

this transformation produces the wanted result:

这种转换产生了想要的结果:

Chamber of Secrets
Prisoners in Azkabahn


阿兹卡巴的密室囚犯

回答by BenAlabaster

In your XML you've got no space between Harry Potter but in your XQuery you've got a space. Just make it match and presto, you'll get data back...

在您的 XML 中,您在 Harry Potter 之间没有空格,但在您的 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

回答by Johan L

The XPath expression

XPath 表达式

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

will return a node set with all text nodes containing the value "HarryPotter:". To get the book title succeeding "HarryPotter:" you need to go through this node set and fetch that substring for each node. This can be done with substring-after if you're using XSLT or with Substring and IndexOf if you're using VB as shown by balabaster.

将返回一个节点集,其中所有文本节点都包含值“HarryPotter:”。要在“HarryPotter:”之后获得书名,您需要遍历此节点集并为每个节点获取该子字符串。如果您使用 XSLT,则可以使用 substring-after 完成此操作,如果您使用 VB,则可以使用 Substring 和 IndexOf 来完成,如balabaster 所示。

回答by Zahra Hnn

Xpath:

X路径:

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

I'm totally new in this area, but for me, it works ...!

我在这方面完全陌生,但对我来说,它有效......!