【问题标题】:Duplicate some parts of XML without rewriting them复制 XML 的某些部分而不重写它们
【发布时间】:2018-05-04 20:41:25
【问题描述】:

我有一个包含重复部分的 XML 文件,例如:

<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>

我希望在 create 和 update 之间声明一次,然后在 create 和 update 之间添加一个单行。这样可以节省很多行数。

有什么方法可以在 XML 中做到这一点?

【问题讨论】:

  • 如果您知道如何使用 XSLT,那可能就是您的答案。但是,XSLT 不适合胆小的人。
  • 我认为您需要使用循环。也许这可以帮助:stackoverflow.com/questions/3462418/…

标签: xml code-duplication


【解决方案1】:

您可以为此使用 SGML/XML“实体”,它可以包含替换文本或标记以便在多个地方重复使用:

<!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>

请注意,您必须调整 DOCTYPE:它必须与 XML 的文档元素匹配。

另见XML configuration inheritance, avoid duplications

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-04
    • 2017-06-26
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多