`
风花雪月饼
  • 浏览: 74140 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

关于Schame的unique和key

阅读更多
最近又需要用到xml.自然的,校验是用schema了.
而常用的XML数据通常会有类似于数据库的ID,一种需要是唯一的数据类型.
那么我就不管那是数字的还是字符的了..
如何在schema里面定义?

下面有一个xml
<?xml version="1.0"?>

<templates
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="TagTemplate.xsd">
        <template >
                <templateStr>
                <![CDATA[
               
                ]]>
                </templateStr>
        </template>
       
        <template >
                <templateStr>
                <![CDATA[
               
                ]]>
                </templateStr>
        </template>
       
</templates>


很简单的一个.
我需要template元素的name属性是唯一的...
OK.
如何定义?

那么我得先假设...你至少知道一点...相关的知识,比如schema的定义.

        <!-- Element[templates] -->
        <xsd:element type="Templates">
                <xsd:unique >
                        <xsd:selector xpath="template" />
                        <xsd:field xpath="@name" />
                </xsd:unique >
        </xsd:element>


以上就是定义了唯一的schema代码了..
呃...让我们来看一下吧...
注意...这个XML供包含三种元素.
templates元素是根元素.
template是根元素下的子元素.
templateStr是template元素下的子元素.

注意了.
<xsd:element color=red>templates" type="Templates">
这里可是定义在templates,根元素里面的哦.
为什么?
因为我是要在根元素下去找template元素的name属性...
那么符合以下的:
<xsd:selector xpath="template" />
<xsd:field xpath="@name" />

而很多初学者呢,就总是误认为是定义在template元素下...
另外...IE好象不提供数据校验.你可能需要有一个XMLSPY或其他提供XML校验的工具...

OK.没了.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics