Ashwini Mate
Ashwini Mate

Reputation: 56

In an xpath script, how to validate absence of an HTML attribute?

<div id="columnmain">
    <h3 class="toggler atStart">
      ....
    <h3 class="toggler atStart" id="H4">
      ....
    <h3 class="toggler atStart" id="H0001">
      ....
    <h3 class="toggler atStart" id="H0000">
      ....
</div>

while fetching the xpath value for first from xml script I have wrote the following:

<children>
    <childrenExpression>
        <expression>//div[@id='columnmain']/h3[@class='toggler atStart']/a</expression>
        <href>./@href</href>
        <values>
            <report.url>./@href</report.url>
            <report.title>./text()</report.title>
        </values>
    </childrenExpression>
</children>

and for fetching rest tags :

<children>
    <childrenExpression>
        <expression>//div[@id='columnmain']/h3[@id='H4']/a</expression>
        <href>./@href</href>
        <values>
            <report.url>./@href</report.url>
            <report.title>./text()</report.title>
        </values>
    </childrenExpression>
</children>

<children>
   <childrenExpression>
       <expression>//div[@id='columnmain']/h3[@id='H0000']/a</expression>
            <href>./@href</href>
            <values>
                <report.url>./@href</report.url>
                <report.title>./text()</report.title>
            </values>
    </childrenExpression>
</children>

<children>
   <childrenExpression>
       <expression>//div[@id='columnmain']/h3[@id='H0001']/a</expression>
            <href>./@href</href>
            <values>
                <report.url>./@href</report.url>
                <report.title>./text()</report.title>
            </values>
    </childrenExpression>
</children>

But when I fetch the first <h3> using the class name, all rest of the tags are also fetched, that is in total 4 tags are being fetched in the first case itself. So if I want to fetch only the first tag using checking the class name and including condition for checking the absence of id. What should I write in the <expression> tag.

Upvotes: 3

Views: 155

Answers (2)

CiaPan
CiaPan

Reputation: 9570

Use fn:empty in a predicate:

h3[ @class = 'toggler atStart' ][ empty(@id) ]

The empty(seq) function returns true if the input sequence is empty, in this case: if there is no id attribute

Upvotes: 0

har07
har07

Reputation: 89315

You can add not(@id) in the predicate expression for h3 to filter out h3 elements with id attribute :

//div[@id='columnmain']/h3[@class='toggler atStart' and not(@id)]/a

Upvotes: 3

Related Questions