Is it possible to apply normalize-space to all nodes XPath expression finds?

后端 未结 2 647
悲哀的现实
悲哀的现实 2020-12-06 11:26

Consider simple XML document:


Item 1
Item 2
相关标签:
2条回答
  • 2020-12-06 11:49

    Using XPath "/html/body/table/tr/td/text()" we will get [" Item 1", " Item 2"].

    Is it possible to trim white space for example using normalize-space() function to get ["Item 1", "Item 2"]?

    Not in XPath 1.0.

    In Xpath 2.0 this is simple:

    /html/body/table/tr/td/text()/normalize-space(.) 
    

    In XPath 2.0 a location step of an XPath expression may be a function reference. This is used in the expression above to produce a sequence of xs:string items, each of which is the result of applying normalize-space() on the context node (any node selected by the subexpression that precedes the last location step).

    0 讨论(0)
  • 2020-12-06 11:52

    If you're using XPath 2.0 you can just use /html/body/table/tr/td/normalize-space(.).

    If you're stuck with XPath 1.0, I don't believe this is possible. You'll just have to loop over the resulting strings and normalize them.

    0 讨论(0)
提交回复
热议问题