Xpath matches with single quotes?

白昼怎懂夜的黑 提交于 2019-12-24 03:22:15

问题


How can I assert an xpath match that contains single quotes within the string to be asserted?

This is my string with value '40' to be asserted.

I assumed to escape the single quote characters with \' but that does not work.

matches( //faultstring[1]/text(), 'This is my string with value \'40\' to be asserted.' )

How is this done properly?


回答1:


Try this

//faultstring[matches(text(),''')]

or

//faultstring[matches(text(),''')]

or

//faultstring[matches(text(),''')]

For a more elegant solution see this post




回答2:


My understanding of this question is that the problem is caused by the need to use nested quotes if the XPath expression is within an XML document.

If this is the case, one can use this XPath expression:

$yourString = "This is my string with value '40' to be asserted."

XSLT - based verification:

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

 <xsl:template match="/">
     <xsl:copy-of select=
     "/* = &quot;This is my string with value &apos;40&apos; to be asserted.&quot;"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<t>This is my string with value '40' to be asserted.</t>

the XPath expression is evaluated and the result of this evaluation is copied to the output:

true


来源:https://stackoverflow.com/questions/12491554/xpath-matches-with-single-quotes

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