Duplicate some parts of XML without rewriting them

♀尐吖头ヾ 提交于 2019-12-02 05:53:52

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/50182776/duplicate-some-parts-of-xml-without-rewriting-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!