如果在XSD文件中有条件 [英] if condition in XSD file

查看:84
本文介绍了如果在XSD文件中有条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以对xml文件使用xsd架构,该文件以下列方式验证xml文件:



1.如果Attibute1值为XYZ那么Attibute2值应该是ABC?

如果Attibute1值是WXYZ那么Attibute2值应该是DEF?



当前xsd示例:



 <   xs:attribute     name   =   Attribute1   使用  =  required   表单  = 不合格   >  
< xs:simpleType >
< xs:restriction base = xs:string >
< xs:枚举 value = XYZ / >
< xs:enumeration value = WXYZ / >
< / xs:restriction >
< / xs:simpleType >
< / xs:attribute >

< xs:attribute 名称 = Attribute2 使用 = required form = 不合格 >
< xs:simpleType >
< xs:restriction base = xs:string >
< xs:enumeration = ABC / >
< xs:enumeration value = DEF / >
< xs:enumeration value = GHI / >
< / xs:restriction >
< / xs:simpleType >
< / xs:attribute >







我添加了以下schematron代码:



 <   xs:element     name   =  ImageView    maxOccurs   =  unbounded    minOccurs   =  1    type   =  ImageView_Type   >  
< xs:annotation >
< xs:appinfo < span class =code-keyword>>

< sch:pattern name = 属性标题上的共现约束 xmlns:sch = http://purl.oc lc.org/dsdl/schematron\">
<! - < sch:rule context =ImageView> - > ;
< sch:rule context = * [@ Attribute1] >
< sch:assert test = (@ Attribute1 ='XYZ'和@ Attribute2 ='ABC') > ; 如果Attribute1为XYZ,那么Attribute2必须为ABC。< / sch:断言 >
< / sch:rule >
< / sch:pattern >
< / xs:appinfo >
< / xs:annotation >





但仍然没有执行验证,我使用MSXML4进行xml验证。

解决方案

 <   xs:element     name   =  ImageView    maxoccurs   =  unbounded    minoccurs   =  1    type   =  ImageView_Type    xmlns:xs   = #unknown >  
< xs:annotation >
< xs:appinfo >
< sch:pattern name = 属性标题上的共现约束 xmlns:sch < span class =code-keyword> = http://purl.oclc.org/dsdl/schematron >
<! - < sch:rule context =ImageView> - >
< xs:attribute 名称 = Attribute1 使用 = required form = 不合格 >
< xs:simpletype >
< xs:restriction base = xs:string >
< xs:枚举 value = XYZ / >
< xs:enumeration value = WXYZ / >
< / xs:restriction >
< / xs:simpletype >
&l t; / xs:attribute >

< xs:attribute name = Attribute2 使用 = required 表格 = 不合格 >
< xs:simpletype >
< xs:r estriction base = xs:string >
< xs:enumeration value = ABC / >
< xs:enumeration value = DEF / >
< xs:enumeration value = GHI / >
< / xs:restriction >
< / xs:simpletype >
< / xs:attribute >
< sch:rule context = * [@ Attribute1] >
< sch:assert test = (@ Attribute1 ='XYZ'和@ Attribute2 ='ABC') > 如果Attribute1为XYZ则Attribute2必须是ABC。< / sch:assert >
< / sch:rule >
< / sch:pattern >
< / xs:appinfo >
< / xs:annotation >
< / xs:element >


Is it possible to use a xsd schema against an xml file that validates the xml file in the following ways:

1. if Attibute1 value is "XYZ" then Attibute2 value should be "ABC"?
if Attibute1 value is "WXYZ" then Attibute2 value should be "DEF"?

current xsd sample:

<xs:attribute name="Attribute1" use="required" form="unqualified" >
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value="XYZ" />
                    <xs:enumeration value="WXYZ" />
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>

        <xs:attribute name="Attribute2" use="required" form="unqualified" >
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value="ABC" />
                    <xs:enumeration value="DEF" />
                    <xs:enumeration value="GHI" />
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>




I added the following schematron code:

<xs:element name="ImageView" maxOccurs="unbounded" minOccurs="1" type="ImageView_Type" >
<xs:annotation>
    <xs:appinfo>
        <sch:pattern name="Co-occurrence constraint on attribute Title" xmlns:sch="http://purl.oclc.org/dsdl/schematron">
        <!--<sch:rule context="ImageView">-->
        <sch:rule context="*[@Attribute1]">
        <sch:assert test="(@Attribute1 = 'XYZ' and @Attribute2 = 'ABC')">If the Attribute1 is "XYZ" then the Attribute2 must be "ABC".</sch:assert>
        </sch:rule>
        </sch:pattern>
    </xs:appinfo>
</xs:annotation>



but still it is not performing the validation , i am using MSXML4 for xml validation.

解决方案

<xs:element name="ImageView" maxoccurs="unbounded" minoccurs="1" type="ImageView_Type" xmlns:xs="#unknown">
    <xs:annotation>
        <xs:appinfo>
            <sch:pattern name="Co-occurrence constraint on attribute Title" xmlns:sch="http://purl.oclc.org/dsdl/schematron">
                <!--<sch:rule context="ImageView">-->
                <xs:attribute name="Attribute1" use="required" form="unqualified">
                    <xs:simpletype>
                        <xs:restriction base="xs:string">
                            <xs:enumeration value="XYZ" />
                            <xs:enumeration value="WXYZ" />
                        </xs:restriction>
                    </xs:simpletype>
                </xs:attribute>
            
                <xs:attribute name="Attribute2" use="required" form="unqualified">
                    <xs:simpletype>
                        <xs:restriction base="xs:string">
                            <xs:enumeration value="ABC" />
                            <xs:enumeration value="DEF" />
                            <xs:enumeration value="GHI" />
                        </xs:restriction>
                    </xs:simpletype>
                </xs:attribute>
                <sch:rule context="*[@Attribute1]">
                    <sch:assert test="(@Attribute1 = 'XYZ' and @Attribute2 = 'ABC')">If the Attribute1 is "XYZ" then the Attribute2 must be "ABC".</sch:assert>
                </sch:rule>
            </sch:pattern>
        </xs:appinfo>
    </xs:annotation>
</xs:element>


这篇关于如果在XSD文件中有条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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