How to use the “translate” Xpath function on a node-set

后端 未结 3 861
离开以前
离开以前 2021-01-02 18:49

I have an XML document that contains items with dashes I\'d like to strip

e.g.


   
      a-         


        
3条回答
  •  天涯浪人
    2021-01-02 18:56

    The translate function accepts in input a string and not a node-set. This means that writing something like:

    "translate(/xmlDoc/items/item/text(),'-','')"
    

    or

    "translate(/xmlDoc/items/item,'-','')"
    

    will result in a function call on the first node only (item[1]).

    In XPath 1.0 I think you have no other chances than doing something ugly like:

    "concat(translate(/xmlDoc/items/item,'-',''),
     translate(/xmlDoc/items/item[2],'-',''))"
    

    Which is privative for a huge list of items and returns just a string.


    In XPath 2.0 this can be solved nicely using for expressions:

      "for $item in /xmlDoc/items/item  
        return replace($item,'-','')"
    

    Which returns a sequence type:

    abc cde
    

    PS Do no confuse function calls with location paths. They are different kind of expressions, and in XPath 1.0 can not be mixed.

提交回复
热议问题