How to get the XPath result without any commas or square brackets?

て烟熏妆下的殇ゞ 提交于 2019-12-12 04:19:00

问题


I have a code such as:

<out:a>
<out:Name Type="First" TypeCode="Best">JAE</out:Name>
<out:Name Type="Last" TypeCode="Best">ADAMS</out:Name>
</out:a>

When I gave the XPath expression as

(//*[local-name() = 'Name']/text())[1],(//*[local-name() = 'Name']/text())[2], 

I got the result as [JAE,ADAMS]

How can I give the XPath expression so that I can the result as JAE ADAMS?


回答1:


Your question is unclear as to where you are using this XPath expression.

In most cases you could use something like:

concat(//*:Name[@Type='First'], " ", //*:Name[@Type='Last'])

In SoapUI XPath assertion, the concatenation is performed automatically, so just a simple:

${#Response#//*:Name[@Type='First']} ${#Response#//*:Name[@Type='Last'])}

(note the extra space) will work.




回答2:


Possibly string-join will do. Try:

string-join( ((//*[local-name() = 'Name']/text())[1],(//*[local-name() = 'Name']/text())[2]), ' ' )

or

string-join( (//*[local-name() = 'Name']/text())[position() le 2], ' ' )

or

string-join( subsequence( //*[local-name() = 'Name']/text(), 1, 2), ' ')



回答3:


The commas and square brackets are a notation that indicates that the result is a node set or sequence. You apparently would prefer a string.

First, however, note that your XML is not well-formed; it uses an undeclared namespace prefix. Here is your XML fixed to be well-formed:

<out:a xmlns:out="example.com/out">
  <out:Name Type="First" TypeCode="Best">JAE</out:Name>
  <out:Name Type="Last" TypeCode="Best">ADAMS</out:Name>
</out:a>

Then, any of these XPath expressions

  • XPath 1.0

    concat(/*/*[1], ' ', /*/*[2])
    
  • XPath 1.0 (namespace-responsible)

    concat(//out:Name[1], ' ', //out:Name[2])
    
  • XPath 2.0

    string-join(/*/*, ' ')
    
  • XPath 2.0

    string-join(normalize-space(), ' ')
    

Will yield the string

JAE ADAMS

you request.



来源:https://stackoverflow.com/questions/34754733/how-to-get-the-xpath-result-without-any-commas-or-square-brackets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!