根据 XSD 中的属性值使 XML 元素成为必需 [英] Make XML elements required based on attribute values in XSD

查看:30
本文介绍了根据 XSD 中的属性值使 XML 元素成为必需的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是有一个 XSD 文件,它根据属性值检查元素.我能够将 XSD 编写到可以限制 Application/@Type 的属性值的程度.任何人都可以帮助我完成 XSD 文件,我可以在其中根据 Application/@Type 属性制作一些必需的元素吗?

My requirement is to have an XSD file which checks the elements based on attribute values. I was able to write XSD up to a point where I can restrict the attribute values of Application/@Type. Can anyone help me to complete the XSD file where I can make some elements required based on the Application/@Type attribute?

我想做

  • PackageArg 仅当 Application/@Type 为 "Batch" 时才需要
  • Version 仅当 Application/@Type 为服务"时才需要
  • Project 仅当 Application/@Type 是 Web" 或 "Service" 时才需要
  • PackageArg required only when Application/@Type is "Batch"
  • Version required only when Application/@Type is "Service"
  • Project required only when Application/@Type is Web" or "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问题的答案现在变成:

Update: OP has edited question to remove the use of xs:assert and has stated in comments that validation must take place in C#. The answer to OP's question now becomes:

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

(保留以供未来读者使用)

(Retained for benefit of future readers)

你很接近,但你的断言,

You're close, but your assertion,

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

测试 PackageArg 上的 @type 何时应该在 Application 上测试 @Type.

tests for @type on PackageArg when it should test for @Type on Application.

以下 XSD 将验证您的 XML 并强制执行您的属性相关要求:

The following XSD will validate your XML and enforce your attribute-dependent requirements:

<?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"标记了您的问题).

Be aware that Microsoft does not support XSD 1.1. (You've tagged your question with 'msxml').

这篇关于根据 XSD 中的属性值使 XML 元素成为必需的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆