开发者

How to use two xpath inside xpath contains function

开发者 https://www.devze.com 2023-02-17 00:51 出处:网络
I have one dropdown box like so: <html> <body> <div class=\"dropdownlist\"> <select name=\"childVariationASIN\" style=\"width:180px;\">

I have one dropdown box like so:

<html>
<body>
<div class="dropdownlist">

<select name="childVariationASIN" style="width:180px;"> 


<option value="" selected="selected" title="Select">Select</option>
<option value="B002VYJB5S" title="59 mm :: 101/13 Gold-Cream/Brown Gradient">Gold-Cre...</option> 
<option value="B00193CBMM" title="59 mm :: 103/11 Gunmetal-Grey/Grey Gradient">Gunmetal...</option> 
<option value="B003ENRWHE" title="59 mm :: 103/13 Gunmetal-Gre开发者_JS百科y/Brown">Gunmetal...</option> 

</select>
</div>
<div id='selected_product_name'>
Gold-Cream/Brown Gradient
<div>


</body>
</html>

And have another div which displays product names, e.g. "Gold-Cream/Brown Gradient".

Now I need to get the option value="B002VYJB5S" by defining XPath expression which checks that product name div against the option title attribute. Something like this:

<xpath expression="//select[@name='childVariationASIN']/option[@value!='' and @title[contains(//div[@id='selected_product_name']/text()) " >

This "contains" part dosen't work. How can I get that corresponding option value "B002VYJB5S"?

and i am not sure about that xpath expression is correct one

Thanks Ram


Something like this:

<xpath expression=
"//select[@ name='childVariationASIN']
          /option[@value!='' 
                and 
                 @title[contains(.,//div[@id='selected_product_name']/text())

" >

This "contains" part dosen't work.

Without showing us the XML document against which the XPath expression is evaluated, we can only try to guess the reason...

It is likely that

//div[@id='selected_product_name']/text()

selects more than one text nodes (such as white-space-only text nodes) and the wanted text node doesn't happen to be the first.

Or, that the wanted text node is not a child of the div, but just its descendent.

Or, ...

UPDATE: The OP has now showed us the XML document.

We happily and instantaneously find the problem and fix the XPath expression:

//select[@ name='childVariationASIN']
   /option[@value!=''
          and
           @title
             [contains(.,normalize-space(//div[@id
                                              =
                                              'selected_product_name'
                                              ]
                                              /text()
                                              )
                       )
              ]
          ]

If you don't believe this expression selects the wanted node, here is a simple verification to try:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:copy-of select=
  " //select[@ name='childVariationASIN']
       /option[@value!=''
              and
               @title
                 [contains(.,normalize-space(//div[@id
                                                  =
                                                  'selected_product_name'
                                                  ]
                                                  /text()
                                             )
                           )
                  ]
             ]
  "/>
 </xsl:template>
</xsl:stylesheet>

when applied against the provided XML document:

<html>
    <body>
        <div class="dropdownlist">
            <select name="childVariationASIN" style="width:180px;">
                <option value="" selected="selected" title="Select">Select</option>
                <option value="B002VYJB5S" title="59 mm :: 101/13 Gold-Cream/Brown Gradient">Gold-Cre...</option>
                <option value="B00193CBMM" title="59 mm :: 103/11 Gunmetal-Grey/Grey Gradient">Gunmetal...</option>
                <option value="B003ENRWHE" title="59 mm :: 103/13 Gunmetal-Grey/Brown">Gunmetal...</option>
            </select>
        </div>
        <div id='selected_product_name'> 
               Gold-Cream/Brown Gradient 
             </div>
    </body>
</html>

the wanted, correct result is produced:

<option value="B002VYJB5S" 
   title="59 mm :: 101/13 Gold-Cream/Brown Gradient">Gold-Cre...</option>

Explanation:

The text node child of the div element that we want our selected node to contain is:

<div id='selected_product_name'> 
Gold-Cream/Brown Gradient 
</div>

The text is:

" 
Gold-Cream/Brown Gradient 
"

It obviously contains a starting and ending NL characters -- but they are not contained in the title attribute of the option element that we want to select.

The standard XPath function normalize-space(), among other things, removes all starting and ending whitespace from a string -- we use it and its result is really contained in the title attribute of the option element.

0

精彩评论

暂无评论...
验证码 换一张
取 消