Relax NG 架构中的 xsd:ID

发布于 2024-10-18 12:30:56 字数 673 浏览 5 评论 0原文

我有以下 xml 文件

<bookshop>
<book bid="1"> Programming in C# </book>
<book bid="2"> programming in Java </book>
<authors>
 <author bidref="1"> person1 </author>
 <author bidref="2"> person2 </author>
 <author bidref="1"> person3 </author>
</authors>
</bookshop>

,然后我制作了以下 Relax NG 模式

start=element bookshop{
element book {attribute bid{xsd:ID},
              text}
element authors{
    element author { attribute bidref{xsd:IDREF},
                         text}
               }}

但是,它总是给我错误,说属性 bid 的值无效必须是不带冒号的 XML 名称

I have the following xml file

<bookshop>
<book bid="1"> Programming in C# </book>
<book bid="2"> programming in Java </book>
<authors>
 <author bidref="1"> person1 </author>
 <author bidref="2"> person2 </author>
 <author bidref="1"> person3 </author>
</authors>
</bookshop>

then I made the following Relax NG schema

start=element bookshop{
element book {attribute bid{xsd:ID},
              text}
element authors{
    element author { attribute bidref{xsd:IDREF},
                         text}
               }}

However, it always gives me error said that value of attribute bid is invalid must be an XML name without colons

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清醇 2024-10-25 12:30:56

好的,我已经修复了您的 XML 示例中的错误。您的模式无法验证您在那里提供的 XML,因为它是错误的。无论如何,这可能至少部分是复制和粘贴错误。我认为你的意思是下面的模式(插入一个或多个标记和序列逗号):

start=

element bookshop
{
    element book {attribute bid {xsd:ID}, text}+,

    element authors
    {
        element author { attribute bidref {xsd:IDREF}, text}
    }

}

顺便说一句,这种“俄罗斯娃娃”模式是非常难以维护的。如果您使用 RelaxNG,那么最好使用命名模式。

现在,您的根本问题是您已将属性 bidbidref 分别建模为 IDIDREF 。这些类型让人想起 DTD。 ID 类型被定义为与“名称”生产相匹配其定义(在同一文档中)为:

NameStartChar ::=       ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | 
    [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | 
    [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | 
    [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | 
    [#x203F-#x2040]
Name     ::= NameStartChar (NameChar)*

简单地说,“您不能以数字开头 ID,ID 也不能只是数字”。 XML ID(和 IDREF)值必须以字母开头。

顺便说一句,您的架构可能更好地表示为:

bookshop.content = (book+, authors)
bookshop = element bookshop {bookshop.content}

book.bid = attribute bid {xsd:ID}
book.content = (book.bid, text)
book = element book {book.content}

authors.content = author+ 
authors = element authors {authors.content}

author.bidref = attribute bidref {xsd:IDREF}
author.content = (author.bidref, text)
author = element author {author.content}

OK, I've fixed the errors in your XML example. Your schema can't validate the XML you've given there because it's, well, wrong. Anyway, that's probably at least partly a copy and paste error. The schema I think you mean is below (one or more marker and sequence comma inserted):

start=

element bookshop
{
    element book {attribute bid {xsd:ID}, text}+,

    element authors
    {
        element author { attribute bidref {xsd:IDREF}, text}
    }

}

As an aside, this sort of 'russian doll' schema is horribly unmaintainable. If you are using RelaxNG you'd be better off with named patterns.

Now, your fundamental problem here is that you've modelled the attributes bid and bidref as ID and IDREF respectively. These types hark back to DTDs. An ID type is defined as matching the 'Name' production which is defined (in the same document) as:

NameStartChar ::=       ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | 
    [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | 
    [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | 
    [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | 
    [#x203F-#x2040]
Name     ::= NameStartChar (NameChar)*

In simple terms that says "you can't start an ID with a number nor can an ID be just a number". XML ID (and IDREF) values must start with a letter.

Your schema, btw, might be better expressed as:

bookshop.content = (book+, authors)
bookshop = element bookshop {bookshop.content}

book.bid = attribute bid {xsd:ID}
book.content = (book.bid, text)
book = element book {book.content}

authors.content = author+ 
authors = element authors {authors.content}

author.bidref = attribute bidref {xsd:IDREF}
author.content = (author.bidref, text)
author = element author {author.content}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文