重复使用行集的 XML 元素与语义标记

发布于 2024-12-11 07:39:15 字数 1807 浏览 0 评论 0原文

我们需要提供一个 Web 服务,它从数据库中提取行集(可能是来自子游标的嵌套行)。

下面哪些是更干净的方法。我更喜欢语义方法,尽管使用通用元素可能有优势。

选项 1:

为列表中的每个元素重复使用“Row”元素。可能有助于自动化(在遇到时将行转换为列表)。 XSD 会是什么样子?

            <Data1 rows="2">
                <Row num="1"><Data11>Data</Data11></Row>
                <Row num="2"><Data11>Data</Data11></Row>
            </Data1>

            <Data2 rows="3">
                <Row num="1"><Data21>Data</Data21></Row>
                <Row num="2">                   
                    <Data3 rows="1">
                        <Row num="1">
                            <Data4>Data</Data4>
                            <Data5>Data</Data5>
                        </Row>
                    </Data3>
                </Row>
            </Data2>    

不要重复使用元素。维护语义结构(忽略下面的概括,它们将被有意义的元素替换)

            <Data1 rows="2">
                <Data1Row num="1"><Data11>Data</Data11></Data1Row>
                <Data1Row num="2"><Data12>Data</Data12></Data1Row>
            </Data1>

            <Data2 rows="3">
                <Data2Row num="1"><Data21>Data</Data21></Data2Row>
                <Data2Row num="2">
                    <Data3 rows="1">
                        <Data3Row num="1">
                            <Data4>Data</Data4>
                            <Data5>Data</Data5>
                        </Data3Row>
                    </Data3>
                </Data2Row>
            </Data2>    

We need to provide a webservice, which extracts row sets from a DB (Could be nested rows coming from a sub cursor).

Which ones below is a cleaner approach. I prefer the semantic approach though there might be advantages in using generic elements.

Option 1:

Reuse the "Row" element for each element that is a list. Might help automation (transform Row to a list on encountering). How would the XSD look like?

            <Data1 rows="2">
                <Row num="1"><Data11>Data</Data11></Row>
                <Row num="2"><Data11>Data</Data11></Row>
            </Data1>

            <Data2 rows="3">
                <Row num="1"><Data21>Data</Data21></Row>
                <Row num="2">                   
                    <Data3 rows="1">
                        <Row num="1">
                            <Data4>Data</Data4>
                            <Data5>Data</Data5>
                        </Row>
                    </Data3>
                </Row>
            </Data2>    

Do not reuse elements. Maintain a semantic structure (Ignore the generalizations below, they will be replaced by meaningful elements)

            <Data1 rows="2">
                <Data1Row num="1"><Data11>Data</Data11></Data1Row>
                <Data1Row num="2"><Data12>Data</Data12></Data1Row>
            </Data1>

            <Data2 rows="3">
                <Data2Row num="1"><Data21>Data</Data21></Data2Row>
                <Data2Row num="2">
                    <Data3 rows="1">
                        <Data3Row num="1">
                            <Data4>Data</Data4>
                            <Data5>Data</Data5>
                        </Data3Row>
                    </Data3>
                </Data2Row>
            </Data2>    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁梦 2024-12-18 07:39:15

我会质疑你的整体设计。无论您是否重复使用 Row 类型,您的设计仍然具有很大的限制性,并且将使将来的更改变得更加困难。

为什么不让 Data 类型也可重用?

该模式可能如下所示:

<xs:schema xmlns="http://MySchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://MySchema" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="MyRoot">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Data" type="Data" maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <!-- Type definition: Data -->
  <xs:complexType name="Data" mixed="true">
    <xs:sequence>
      <xs:element name="Row" type="Row" minOccurs="0" />
    </xs:sequence>
    <xs:attribute name="rows" type="xs:int" />
  </xs:complexType>
  <!-- Type definition: Row -->
  <xs:complexType name="Row">
    <xs:sequence>
      <xs:element name="Data" type="Data" minOccurs="1" />
    </xs:sequence>
    <xs:attribute name="num" type="xs:int" />
  </xs:complexType>
</xs:schema>

这将允许您对更广泛的数据结构变化进行建模。

更新

阅读了您的以下评论后,我会说做相反的事情。您应该将每个实体(人、城市等)建模为单独的 XSD 复杂类型。仅当存在与重用相关的任何内容时,重用才有好处,并且我不认为拥有一个实际上可以代表许多不同事物的通用 Row 类型作为重用的良好候选者。

如果您希望我扩展此内容,我很乐意这样做。

I would question your overall design. Whether you re-use the Row type or not, your design is still very restrictive and will make future change much more difficult.

Why not have the Data type also reusable?

The schema could then look like:

<xs:schema xmlns="http://MySchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://MySchema" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="MyRoot">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Data" type="Data" maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <!-- Type definition: Data -->
  <xs:complexType name="Data" mixed="true">
    <xs:sequence>
      <xs:element name="Row" type="Row" minOccurs="0" />
    </xs:sequence>
    <xs:attribute name="rows" type="xs:int" />
  </xs:complexType>
  <!-- Type definition: Row -->
  <xs:complexType name="Row">
    <xs:sequence>
      <xs:element name="Data" type="Data" minOccurs="1" />
    </xs:sequence>
    <xs:attribute name="num" type="xs:int" />
  </xs:complexType>
</xs:schema>

This would allow you to model a much wider variation of data structure.

UPDATE

Having read your below comments I would say do the opposite. You should be modelling each entity (person, city, etc) as a separate XSD complex type. Re-use is only of benefit if there is anything relevant to re-use, and I can't see that having a generic Row type which can actually represent many different things as a good candidate for re-use.

If you want me to expand this I am happy to do so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文