微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我的要求是有一个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>WebServices\ConfigService</ProjectDirName>
        <Project>ConfigService.csproj</Project>    
        <Version>2015\04</Version>
        <OutputDirName>ConfigService</OutputDirName>
    </Application>
    <Application Name="DeliverEmail" Type="Batch">        
        <ProjectDirName>\Batch\DeliverEmail</ProjectDirName>
        <PackageArg>Release</PackageArg>        
        <OutputDirName>Tidal\DeliverEmail</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’标记了您的问题).

原文地址:https://www.jb51.cc/xml/293084.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。