XSD:如何根据某个标签的值来验证 XML 文件? [英] XSD: How to validate the XML file according to value of some tag?

查看:29
本文介绍了XSD:如何根据某个标签的值来验证 XML 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图验证这个 XML 文件...如果

I was trying to validate this XML file ... where if

  1. 是Y"然后 必须出现

  1. <tag1> is "Y" then <tag2> must appear

如果 是N",则 不得出现..

if <tag1> is "N" then <tag2> must not appear ..

<parent>
  <a>
    <tag1>Y</tag1>
    <tag2>sometext</tag2>
  </a>
  <a>
    <tag1>N</tag1>
  </a>
</parent>

我尝试了 <choice> 标记,但似乎不起作用.. :( 我得出的结论是此功能在 XSD 中不可用..

I tried <choice> tag but doesn't seem to work .. :( I have come to conclusion that this feature is not available in XSD ..

你能指导我至少有一些替代方案来实现吗?顺便说一下,我使用的是 Visual Studio 2005 ..

Can you guide me atleast some alternative to implement this ? by the way I am using Visual Studio 2005 ..

推荐答案

众所周知,这是 XML 模式的一个方便之处.但我很感激你尝试使用 标签的方法.如果您的条件是这样的,它可能会成功:

It's a known fact that this is a handycap of XML schema. But I would appreciate your approach of trying the <choice> tag. It could be successful if your conditions were something like this:

  1. 如果 是必需的并且首先出现,则 不是必需的(并且出现为第二个标签)
  2. 如果 是必需的并且第一个出现,那么 不是必需的(并且出现在第二个)
  1. If <tag1> is required and appears first then <tag2> isn't required (and appears as second tag)
  2. If <tag2> is required and appears first then <tag1> isn't required (and appears as second)

代码是:

<xs:element name="parent">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="a" maxOccurs="unbounded">
        <xs:complexType>
          <xs:choice>
            <xs:sequence>
              <xs:element name="tag1" type="xs:boolean" />
              <xs:element name="tag2" type="xs:string" minOccurs="0" />
            </xs:sequence>
            <xs:sequence>
              <xs:element name="tag2" type="xs:string" />
              <xs:element name="tag1" type="xs:boolean" minOccurs="0" />
            </xs:sequence>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

这篇关于XSD:如何根据某个标签的值来验证 XML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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