0

The comment is before the root node and I want to delete the "comment 1" line.

<Form>
    <FormBody>
        <!--comment 1-->
        <Label>
          <Style>IVLabel</Style>
          <Caption>lblLanguage</Caption>
          <MarkAsRequired>true</MarkAsRequired>
          <Row>14</Row>
          <Column>2</Column>
        </Label>
    </FormBody>
<Form>
Yashi Arya
  • 11
  • 1
  • The comment is not before the root node because the root node is the root of the entire tree, containing all other nodes, including the offending comment node. It is before the root *element*, though. This matters because it helps understand what queries like `/` and `/Label` select. In this case `/*` would select all named elements in the root node, which is the root element, ignoring any preceding comments or processing instructions, `/comment()[1]` would select the first comment, and `/comment()[.='comment 1'][1]` would specifically select only the first comment with that text. – Jeroen Mostert Oct 21 '22 at 17:23
  • I updated the code section to add the nodes. I tried running xml.modify(delete (/Form/FormBody/comment[.="comment 1"])[1]). Executes successfully however the comment was not deleted. Am I missing something here? – Yashi Arya Oct 21 '22 at 17:30
  • `comment()` is a function -- `comment` selects an element named `comment`. So you want `delete /Form/FormBody/comment()[.="comment 1"][1]` (parentheses not required). Note that you can also just use `delete //comment()` if you're not interested in having *any* comment nodes. – Jeroen Mostert Oct 21 '22 at 17:32

0 Answers0