Skip to content
Advertisement

XPath syntax to edit specific node in HTML using xmlstarlet

Supposing I have an HTML document like what’s below:

mypage.html

<html xmlns="http://www.w3.org/1999/xhtml">
  <body>
    <table>
      <tbody>
       ...
       <tr>
         <td id="MY_ID">123</td>

How would I edit the element set to MY_ID? I’ve used the following command successfully when it was just the table in a document, but placing it in a larger document broke it:

xmlstarlet ed --update '//td[@id="MY_ID"]' --value '456' mypage.html

Advertisement

Answer

Your td element needs to be closed (</td>) for it to be valid XML. You can try the following it here :

<html xmlns="http://www.w3.org/1999/xhtml">
  <body>
    <table>
      <tbody>
       ...
       <tr>
         <td id="MY_ID">123</td>
         <td> id="NOTMYID">127</td>
        </tr>
      </tbody>
    </table>
  </body>
</html>

Using your own expression: //td[@id="MY_ID

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement