找到你要的答案

Q:xpath query checked radio button parent label text inside [duplicate]

Q:XPath查询选中单选按钮的标签文本内[复制]母

This question already has an answer here:

i need to grab the text inside a label with a specific class that has a checked radio input inside it.

This is the HTML:

<div id="ships-from2">

    <label for="ship_hk_intl">
        <input type="radio" name="ship_mode_name" id="ship_hk_intl" data-action="http://www.example.com/" value="hk_intl">
            Hong Kong Warehouse - USD44.31
    </label>

    <label for="ship_us_intl">
        <input type="radio" name="ship_mode_name" id="ship_us_intl" data-action="http://www.example.com/" checked value="us_intl">
            United States Warehouse - USD45.10
    </label>

</div>

.
I need:
the string inside the label that has a checked radio button. The actuall radio button might change so i need to check which one is checked

I am scraping the dom and using xpath but have no idea how to write the query Ideas anyone?

EDIT 1 - CODE THUS FAR (response to @TimDev):

    $div        = $dom->getElementById('ships-from2');
    $query      = '//input[@checked]/../text()';
    $e          = $xpath->query($query, $div);
    echo 'TEST:'.trim($e->item(1)->nodeValue);

这个问题在这里已经有了答案:

i need to grab the text inside a label 具有 a specific class that has a checked radio input inside it.

这是HTML:

<div id="ships-from2">

    <label for="ship_hk_intl">
        <input type="radio" name="ship_mode_name" id="ship_hk_intl" data-action="http://www.example.com/" value="hk_intl">
            Hong Kong Warehouse - USD44.31
    </label>

    <label for="ship_us_intl">
        <input type="radio" name="ship_mode_name" id="ship_us_intl" data-action="http://www.example.com/" checked value="us_intl">
            United States Warehouse - USD45.10
    </label>

</div>

.
I need:
the string inside the label that has a checked radio button. The actuall radio button might change so i need to check which one is checked

I am scraping the dom and using xpath but have no idea how to write the query Ideas anyone?

编辑1码为止(响应@ timdev):

    $div        = $dom->getElementById('ships-from2');
    $query      = '//input[@checked]/../text()';
    $e          = $xpath->query($query, $div);
    echo 'TEST:'.trim($e->item(1)->nodeValue);
answer1: 回答1:

You might need to tweak the query a little but it does return radio input fields and you can rasily check for required attributes.

    $html='
        <div id="ships-from2">
            <label for="ship_hk_intl">
                <input type="radio" name="ship_mode_name" id="ship_hk_intl" data-action="http://www.example.com/" value="hk_intl">
                    Hong Kong Warehouse - USD44.31
            </label>
            <label for="ship_us_intl">
                <input type="radio" name="ship_mode_name" id="ship_us_intl" data-action="http://www.example.com/" checked value="us_intl">
                    United States Warehouse - USD45.10
            </label>
        </div>';

        $dom=new DOMDocument;
        $dom->loadHTML( $html );
        $xpath=new DOMXPath( $dom );
        $col=$xpath->query('//label/input');

        foreach( $col as $node ) if( $node->hasAttribute('checked') ) {
            echo $node->getAttribute('value').' '.$node->parentNode->nodeValue;
        }
        $dom=null;
        $xpath=null;

你可能需要调整查询一点但它返回无线输入字段你可以随便检查所需的属性。

    $html='
        <div id="ships-from2">
            <label for="ship_hk_intl">
                <input type="radio" name="ship_mode_name" id="ship_hk_intl" data-action="http://www.example.com/" value="hk_intl">
                    Hong Kong Warehouse - USD44.31
            </label>
            <label for="ship_us_intl">
                <input type="radio" name="ship_mode_name" id="ship_us_intl" data-action="http://www.example.com/" checked value="us_intl">
                    United States Warehouse - USD45.10
            </label>
        </div>';

        $dom=new DOMDocument;
        $dom->loadHTML( $html );
        $xpath=new DOMXPath( $dom );
        $col=$xpath->query('//label/input');

        foreach( $col as $node ) if( $node->hasAttribute('checked') ) {
            echo $node->getAttribute('value').' '.$node->parentNode->nodeValue;
        }
        $dom=null;
        $xpath=null;
answer2: 回答2:

with xpath you can do like this

//input[@checked]/..

to get text

//input[@checked]/../text()

function test(field) {
  console.log(field.parentElement.innerText);
}
<div id="ships-from2">

    <label for="ship_hk_intl">
        <input type="radio" onchange="test(this)" name="ship_mode_name" id="ship_hk_intl" data-action="http://www.example.com/" value="hk_intl">
            Hong Kong Warehouse - USD44.31
    </label>

    <label for="ship_us_intl">
        <input type="radio" onchange="test(this)"  name="ship_mode_name" id="ship_us_intl" data-action="http://www.example.com/" checked value="us_intl">
            United States Warehouse - USD45.10
    </label>

</div>

XPath可以这样做

//input[@checked]/..

把文本

//input[@checked]/../text()

function test(field) {
  console.log(field.parentElement.innerText);
}
<div id="ships-from2">

    <label for="ship_hk_intl">
        <input type="radio" onchange="test(this)" name="ship_mode_name" id="ship_hk_intl" data-action="http://www.example.com/" value="hk_intl">
            Hong Kong Warehouse - USD44.31
    </label>

    <label for="ship_us_intl">
        <input type="radio" onchange="test(this)"  name="ship_mode_name" id="ship_us_intl" data-action="http://www.example.com/" checked value="us_intl">
            United States Warehouse - USD45.10
    </label>

</div>
answer3: 回答3:

I'm not sure why raghavendra gives javascript, but here's a PHP example. He is right using //input[@checked]/../text().

Note: ../text() is returning two items of text. It returns all the surrounding text of the input node. Which is also the whitespaces between <label> and <input>.

That's why in the below snippet we get the second text with $e->item(1)->nodeValue

$html = <<<EOC
<div>
    More HTML!
</div>
<div>
   Even more HTML!
</div>
<div id="ships-from2">
    <label for="ship_hk_intl">
        <input type="radio" name="ship_mode_name" id="ship_hk_intl" data-action="http://www.example.com/"
               value="hk_intl"/>
        Hong Kong Warehouse - USD44.31
    </label>
    <label for="ship_us_intl">
        <input type="radio" name="ship_mode_name" id="ship_us_intl" data-action="http://www.example.com/" checked
               value="us_intl"/>
        United States Warehouse - USD45.10
    </label>
</div>
EOC;

$doc = new DOMDocument();
$doc->loadHTML($html);
$xpathObject = new DOMXPath($doc);

$div = $doc->getElementById('ships-from2');
$query = '//input[@checked]/../text()';
$e = $xpathObject->query($query, $div);
echo trim($e->item(1)->nodeValue);

I'm not sure why raghavendra gives javascript, but here's a PHP example. He is right using //input[@checked]/../text().

注:/()返回的两项的文本。它返回输入节点的所有周围文本。这也是之间的空格标签<;>;和<;输入& gt;。

这就是为什么在下面的代码片段得到第二文本$ E & gt;项目(1)- >;节点

$html = <<<EOC
<div>
    More HTML!
</div>
<div>
   Even more HTML!
</div>
<div id="ships-from2">
    <label for="ship_hk_intl">
        <input type="radio" name="ship_mode_name" id="ship_hk_intl" data-action="http://www.example.com/"
               value="hk_intl"/>
        Hong Kong Warehouse - USD44.31
    </label>
    <label for="ship_us_intl">
        <input type="radio" name="ship_mode_name" id="ship_us_intl" data-action="http://www.example.com/" checked
               value="us_intl"/>
        United States Warehouse - USD45.10
    </label>
</div>
EOC;

$doc = new DOMDocument();
$doc->loadHTML($html);
$xpathObject = new DOMXPath($doc);

$div = $doc->getElementById('ships-from2');
$query = '//input[@checked]/../text()';
$e = $xpathObject->query($query, $div);
echo trim($e->item(1)->nodeValue);
answer4: 回答4:

First of all it's not valid XML. Each attribute must have a value so replace

 ...data-action="http://www.example.com/" checked value="us_intl">

with

 ata-action="http://www.example.com/" checked="true" value="us_intl" />

then your xpath will look like this:

  //input[@checked="true" and  @id="ship_us_intl"]

首先它不是有效的xml。每个属性必须有一个值以便替换

 ...data-action="http://www.example.com/" checked value="us_intl">

具有

 ata-action="http://www.example.com/" checked="true" value="us_intl" />

那么你的XPath将看起来像这样:

  //input[@checked="true" and  @id="ship_us_intl"]
php  xpath