SchemaTron入门一

       我们知道简单控制xml格式的方式有dtd和schema,但是由于的工作的需要又学到另一种控制xml格式的方式,

那就是Schematron方式,这种控制方式比前两种的优势就是,前面的XSD有三种不能描述的限制:

       1).指定属性选择的能力。例如,server-status 元素应该有一个 server-uptime 属性或一个 server-downtime 属性。 

2).将元素和属性分组到模型组中的能力。尽管我们可以使用诸如 xs:sequencexs:choicexs:all 之类的编写器来分组元素,但不能同时对元素和属性两者做相同的处理。例如,不能在一组元素和属性与另一组元素和属性之间创建选择。

3).基于一个元素或属性的值改变内容模型的能力。例如,如果 status 属性的值为 available,那么该元素应该有一个 uptime 子元素,否则就应该有一个 downtime 子元素。此类约束的技术名称是共现 (co-occurrence) 约束。 

        由于XSD的缺点Schematron就应运而生了,首先我们给出一些简单的语法规则:

定义命名空间
<?xml version="1.0"?>
<sch:schema xmlns:sch="htpp://www.ascc.net..">
	<sch:ns uri="http://www.example.org" prefix="ex"/>
	<sch:pattern name="Security Classification">..</sch:pattern>
</sch:schema>

定义规则rule(在属性中一定要加上命名空间的前缀)
<sch:rule context="ex:Para[@classification='top-scret']">
..
</sch:rule>

定义assert也要加上前缀
<sch:assert test="/ex:Document/@classification='top-scret'">
...
</sch:assert>

4.一个schematron文档中包含一个或者多个<sch:pattern name=""></>
一个<sch:pattern>建议包含一个或者多个<sch:rule context=""></>
标签,一个<sch:rule context="">可以包含一个或者多个
<sch:assert test="XPath">Test description</>

那么我们给出以下*.xml和*.sch两个文件的例子:

test.xml(一个符合一个不符合规则的例子.xml):

invalid.xml:
<!--  invalid-document.xml  -->

<?xml version="1.0"?>
<?oxygen SCHSchema="check-classifications.sch" type="xml"?>
<Document xmlns="http://www.example.org"
          classification="confidential">
    <Para classification="unclassified">
          One if by land, two if by sea;
    </Para>
    <Para classification="confidential">
          And I on the opposite shore will be,
          Ready to ride and spread the alarm
    </Para>
    <Para classification="secret">
          Ready to ride and spread the alarm
          Through every Middlesex, village and farm,
    </Para>
    <Para classification="top-secret">
          For the country folk to be up and to arm.
    </Para>
</Document>



valid.xml:

<?xml version="1.0" encoding="UTF-8"?>

<?oxygen SCHSchema="check-classifications.sch" type="xml"?>
<Document xmlns="http://www.example.org"
    classification="top-secret">
    <Para classification="unclassified">
        One if by land, two if by sea;
    </Para>
    <Para classification="confidential">
        And I on the opposite shore will be,
        Ready to ride and spread the alarm
    </Para>
    <Para classification="secret">
        Ready to ride and spread the alarm
        Through every Middlesex, village and farm,
    </Para>
    <Para classification="top-secret">
        For the country folk to be up and to arm.
    </Para>
</Document>


check-classifications.sch:

<?xml version="1.0" encoding="UTF-8"?>

<?oxygen SCHSchema="check-classifications.sch" type="xml"?>
<Document xmlns="http://www.example.org"
    classification="top-secret">
    <Para classification="unclassified">
        One if by land, two if by sea;
    </Para>
    <Para classification="confidential">
        And I on the opposite shore will be,
        Ready to ride and spread the alarm
    </Para>
    <Para classification="secret">
        Ready to ride and spread the alarm
        Through every Middlesex, village and farm,
    </Para>
    <Para classification="top-secret">
        For the country folk to be up and to arm.
    </Para>
</Document>

将上述两个文件放在Oxygen xml IDE中验证即可


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值