加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

根据XSD中的属性值创建所需的XML元素

发布时间:2020-12-16 07:58:30 所属栏目:百科 来源:网络整理
导读:我的要求是有一个XSD文件,它根据属性值检查元素.我能够将XSD编写到可以限制Application / @Type的属性值的位置.任何人都可以帮我完成XSD文件,我可以根据Application / @Type属性制作一些所需的元素吗? 我要实现 只有当Application / @ Type为“Batch”时才
我的要求是有一个XSD文件,它根据属性值检查元素.我能够将XSD编写到可以限制Application / @Type的属性值的位置.任何人都可以帮我完成XSD文件,我可以根据Application / @Type属性制作一些所需的元素吗?

我要实现

>只有当Application / @ Type为“Batch”时才需要PackageArg
>仅当Application / @ Type为“Service”时才需要版本
>仅当Application / @ Type为Web“或”Service“时才需要项目

XML文件

<Applications>        
    <Application Name="ConfigManagement" Type="Web">            
        <ProjectDirName>ConfigManagement</ProjectDirName>
        <Project>Web.ConfigManagement.csproj</Project>
        <OutputDirName>ConfigManagement</OutputDirName>            
    </Application>
    <Application Name="Util" Type="Web">            
        <ProjectDirName>Web</ProjectDirName>
        <Project>Web.csproj</Project>        
        <OutputDirName>Util</OutputDirName>    
    </Application>
    <Application Name="ConfigService" Type="Service">
        <ProjectDirName>WebServicesConfigService</ProjectDirName>
        <Project>ConfigService.csproj</Project>    
        <Version>20154</Version>
        <OutputDirName>ConfigService</OutputDirName>
    </Application>
    <Application Name="DeliverEmail" Type="Batch">        
        <ProjectDirName>BatchDeliverEmail</ProjectDirName>
        <PackageArg>Release</PackageArg>        
        <OutputDirName>TidalDeliverEmail</OutputDirName>            
    </Application>
</Applications>

XSD文件

<xs:element name="Applications" maxOccurs="1">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Application" maxOccurs="unbounded" minOccurs="1">
        <xs:complexType>
          <xs:all>
            <xs:element type="xs:string" name="ProjectDirName"/>
            <xs:element type="xs:string" name="Project" minOccurs="0"/>
            <xs:element type="xs:string" name="Version" minOccurs="0"/>
            <xs:element type="xs:string" name="PackageArg" minOccurs="0"/>
            <xs:element type="xs:string" name="OutputDirName"/>
          </xs:all>
          <xs:attribute type="xs:string" name="Name" use="optional"/>
          <xs:attribute name="Type" use="required">
            <xs:simpleType>
              <xs:restriction base="xs:string">
                <xs:enumeration value="Web"/>
                <xs:enumeration value="Batch"/>
                <xs:enumeration value="Service"/>
              </xs:restriction>
            </xs:simpleType>
          </xs:attribute>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>
更新:OP已编辑问题以删除xs:assert的使用,并在评论中声明验证必须在C#中进行. OP问题的答案现在变为:

您不能使用XSD 1.0强制执行基于属性值改变元素要求的约束,并且Microsoft不支持XSD 1.1,因此您必须放松约束或在XSD之外检查它们.

原XSD 1.1的答案

(留作未来读者的利益)

你很亲密,但你的断言,

<xs:assert test="count(./PackageArg[@type eq 'Batch']) eq 1"/>

当它应该在Application上测试@Type时,对PackageArg上的@type进行测试.

以下XSD将验证您的XML并强制执行依赖于属性的要求:

XSD 1.1

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           vc:minVersion="1.1">
  <xs:element name="Applications">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Application" maxOccurs="unbounded" minOccurs="1">
          <xs:complexType>
            <xs:all>
              <xs:element type="xs:string" name="ProjectDirName"/>
              <xs:element type="xs:string" name="Project" minOccurs="0"/>
              <xs:element type="xs:string" name="Version" minOccurs="0"/>
              <xs:element type="xs:string" name="PackageArg" minOccurs="0"/>
              <xs:element type="xs:string" name="OutputDirName"/>
            </xs:all>
            <xs:attribute type="xs:string" name="Name" use="optional"/>
            <xs:attribute name="Type" use="required">
              <xs:simpleType>
                <xs:restriction base="xs:string">
                  <xs:enumeration value="Web"/>
                  <xs:enumeration value="Batch"/>
                  <xs:enumeration value="Service"/>
                </xs:restriction>
              </xs:simpleType>
            </xs:attribute>
            <xs:assert test="PackageArg or @Type != 'Batch'"/>
            <xs:assert test="Version or @Type != 'Service'"/>
            <xs:assert test="Project or not(@Type = 'Web' or @Type = 'Service')"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

请注意,Microsoft不支持XSD 1.1. (您已使用’msxml’标记了您的问题).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读