首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将元素添加到另一个XML文件中

将元素添加到另一个XML文件中
EN

Stack Overflow用户
提问于 2018-03-25 16:19:55
回答 1查看 19关注 0票数 0

以下是XML-1:

代码语言:javascript
复制
<bookstore>
  <book category="children">
    <title>Harry Potter</title>
    <author>J K. Rowling</author>
  </book>
  <book category="web">
    <title>Learning XML</title>
    <author>Erik T. Ray</author>
  </book>
</bookstore>

如何通过添加两个元素<year><price>来创建基于XML-1的XML-2?它不复制XML-1,而是通过引用或包含它。这种分离对于将XML-1和XML-2分开存储是必要的,而不是在XML-2中复制XML-1中的信息。

为了最终能够创建XML-3:

代码语言:javascript
复制
<bookstore>
  <book category="children">
    <title>Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="web">
    <title>Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

-2模式应该是什么样的?,我无法理解如何使用引用和包含。在这种情况下,我需要使用它们吗?还是需要其他的东西?

EN

回答 1

Stack Overflow用户

发布于 2018-03-25 16:34:50

您可以使用xsd扩展功能:https://www.liquid-technologies.com/xml-schema-tutorial/xsd-extending-types

代码语言:javascript
复制
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="bookstore">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="book" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="title"/>
              <xs:element type="xs:string" name="author"/>
            </xs:sequence>
            <xs:attribute type="xs:string" name="category" use="optional"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

您需要创建一本扩展的书:

代码语言:javascript
复制
<xs:complexType name="ExtendedBook">
    <xs:complexContent>
        <xs:extension base="book">
            <xs:sequence>
               <xs:element type="xs:short" name="year"/>
               <xs:element type="xs:float" name="price"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49478044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档