2

I have an XML file with duplicate parts such as:

<argument name="create">
    <argument name="user" type="text"></argument>
    <argument name="password" type="password"></argument>
    (and so on)
</argument>

<argument name="update">
    <argument name="user" type="text"></argument>
    <argument name="password" type="password"></argument>
    (and so on)
</argument>

I would like to have the part between create and update declared once then append it between create and update with a one-liner. This would save me a lot of lines.

Any way to do that in XML?

Jeremy Pare
  • 177
  • 7
  • If you know how to use XSLT, that is likely your answer. However, XSLT is not for the faint of heart. – Display name May 04 '18 at 20:52
  • I think you need to use a loop. Maybe this can help: https://stackoverflow.com/questions/3462418/standard-for-loops-in-xml?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Gabrielė May 04 '18 at 20:54

1 Answers1

2

You can use SGML/XML"entities" for that, which can contain replacement text or markup for reuse at multiple places:

<!DOCTYPE arguments [
  <!ENTITY user-and-password
   '<argument name="user" type="text"/>
    <argument name="password" type="password"/>'>
]>
<arguments>
  <argument name="create">
    &user-and-password;
  </argument>
  <argument name="update">
    &user-and-password;
  </argument>
</arguments>

Note that you have to adapt the DOCTYPE: it must match the document element of your XML.

See also XML configuration inheritance, avoid duplications

Jeremy Pare
  • 177
  • 7
imhotap
  • 2,275
  • 1
  • 8
  • 16