有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在正则表达式或xpath提取器中通过文本查找选项值

如何从下面的响应中通过正则表达式或xpath从文本中提取选项值

xpath提取器

 //*[@id="form_extsss:fromSerie2"]/option[text()='-DRIVE_ME']/@value

例如:

-DRIVE_ME的选项值为630(期望630为最终值)

响应

    <?xml version="1.0" encoding="UTF-8"?>
<partial-response>
   <changes>
      <update id="form_extsss"><![CDATA[<form id="form_extsss" name="form_extsss" 
      method="post" action="" enctype="multipart/form-data">
        <select id="form_extsss:fromSerie2" name="form_extsss:fromSerie2" size="1" onchange="PrimeFaces.ab({s:&quot;form_extsss:fromSerie2&quot;,e:&quot;valueChange&quot;,p:&quot;form_extsss:fromSerie2&quot;,u:&quot;form_extsss&quot;});" style="width:110px">  <option value="-1">Select Series</option>   
        <option value="1384">-HAD</option>  
        <option value="630">-DRIVE_ME</option>
        </select>                       
                                    </form>]]></update>
      <update id="javax.faces.ViewState"><![CDATA[]]></update>
   </changes>
</partial-response>

共 (3) 个答案

  1. # 1 楼答案

    <option value="630">
    

    以下正则表达式可在正则表达式提取器中使用:-

    <option value="(.\d*)">
    
  2. # 2 楼答案

    检查以下正则表达式是否有助于您的场景:- enter image description here enter image description here

  3. # 3 楼答案

    使用xpath将不起作用,因为CDATA仅表示为字符串

    但是,如果您有可用的xquery,请执行以下操作

    let $x := <partial-response>
        <changes>
          <update id="form_extsss"><![CDATA[<form id="form_extsss" name="form_extsss" method="post" action="" enctype="multipart/form-data">
          <select id="form_extsss:fromSerie2" name="form_extsss:fromSerie2" size="1"  onchange="PrimeFaces.ab({s:&quot;form_extsss:fromSerie2&quot;,e:&quot;valueChange&quot;,p:&quot;form_extsss:fromSerie2&quot;,u:&quot;form_extsss&quot;});" style="width:110px">
            <option value="-1">Select Series</option>   
            <option value="1384">-HAD</option>  
            <option value="630">-DRIVE_ME</option>
          </select>                       
                                    </form>]]></update>
          <update id="javax.faces.ViewState"><![CDATA[]]></update>
        </changes>
      </partial-response>
    
    return parse-xml($x/changes/update/text())/form/select/option/@value