【问题标题】:Creating xsd schema for the following xml为以下 xml 创建 xsd 架构
【发布时间】:2011-08-01 16:22:25
【问题描述】:

我有以下具有 xsd 架构的 xml,但是很差,并且没有用于序列化。

<rulestruct>
     <rule>
         <type name="vector" />
         <ruleident ruleidentifier="" />
         <pattern type="" />
     </rule>
     <rule>
          <type name="expression" />
          <ruleident ruleidentifier="" />
          <pattern type="" />
     </rule>
     <rule>
          <type name="vector" />
          <ruleident ruleidentifier="" />
          <pattern type="" />
     </rule>
     <rule>
          <type name="statement" />
          <ruleident ruleidentifier="" />
          <pattern type="" />
     </rule>
     <rule>
          <type name="statement" />
          <ruleident ruleidentifier="" />
          <pattern type="" />
     </rule>
</rulestruct>

每个规则结构可以有 1.N 条规则。每个规则都可以重复。必须保持秩序。每个规则有 1.N 个元素,有的有 9 个元素,有的有 10、13 个。有 9 种不同的规则类型。

我正在考虑使用元素组来表示每种规则类型,但我不太确定如何映射它。

【问题讨论】:

  • 不清楚您在这里要求什么。您的意思是需要帮助将类序列化为 XML,还是想为您的类创建更好的模式?

标签: c# .net xml xsd


【解决方案1】:

如果您想为各种规则类型保留相同的元素(规则)名称,您定义了抽象元素(属性abstract="true"),它的所有子元素都将拥有xs:ComplextContent&gt;xs:extension,其基本属性等于您的抽象类型名称. 在您的 XML 中,每个规则元素必须具有 xsi:type attrite 来区分元素的具体类型。 解释和例子是here

如果您希望/可以为每种规则类型使用不同的元素名称,您可以使用替换组。您的共同祖先再次由属性抽象定义。具体类型定义为 xs:element ,其属性substitutionGroup 等于共同祖先的名称。 解释和例子是here

【讨论】:

  • 发现了这个,xfront.com/VariableContentContainers.html,它反映了您所描述的内容。我认为方法 3 似乎最接近我正在寻找的方法,这是您的第一种方法。谢谢。鲍勃
【解决方案2】:

你使用 jaxb 吗?使用 xsd,您可以使用继承,如果需要或不需要元素,您可以使用 minOccurs/maxOccurs。

这里是一个例子,我是从头开始写的,没有测试,可能有错误:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
      jaxb:extensionBindingPrefixes="xjc">
 
      <xsd:annotation>
            <xsd:appinfo>
                  <jaxb:globalBindings>
                        <xjc:simple />
                  </jaxb:globalBindings>
            </xsd:appinfo>
      </xsd:annotation>
 
      <xsd:element name="rulestruct" type="PRuleStruct" />
      <xsd:complexType name="PRuleStruct">
            <xsd:sequence>
                  <xsd:element name="rule" type="PRule" minOccurs="1" maxOccurs="unbounded" />
            </xsd:sequence>
       </xsd:complexType>
 
      <xsd:complexType name="PRule">
            <xsd:sequence>
                  <xsd:element name="vector" type="PVector" minOccurs="1" maxOccurs="unbounded" />
            </xsd:sequence>
       </xsd:complexType>

您已经看到,您的问题通常有很多解决方案。我的选择通常是使用继承和尽可能少的标记并使用名称。这是我的目的:

<rules>
<ruleVector ruleidentifier="sample" patternType="sample">
</ruleVector>

 

【讨论】:

  • 对不起,我从来没有把 .net, c# monikers 标签放进去。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-03
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 1970-01-01
相关资源
最近更新 更多