你能在XML模式中使用key / keyref而不是restriction / enumeration吗?

时间:2022-12-14 17:17:25

Suppose we have a stylesheet which pulls in metadata using the key() function. In other words we have instance documents like this:

假设我们有一个样式表,它使用key()函数提取元数据。换句话说,我们有这样的实例文​​档:

<items>

<item type="some_type"/>

<item type="another_type"/>

</items>

and a table of additional data we would like to associate with items during processing:

以及我们希望在处理过程中与项目关联的其他数据表:

<item-meta>

<item type="some_type" meta="foo"/>

<item type="another_type" meta="bar"/>

<item type="yet_another_type" meta="baz"/>

</item-meta>

Finally, suppose we want to do schema validation on the instance document, restricting the type attributes to the set of types which occur in item-meta. So in the schema we want to use key/keyref instead of restriction/enumeration. This is because using restriction/enumeration will require making a separate list of valid type attributes.

最后,假设我们想要对实例文档进行模式验证,将类型属性限制为item-meta中出现的类型集。所以在模式中我们想要使用key / keyref而不是restriction / enumeration。这是因为使用限制/枚举将需要创建有效类型属性的单独列表。

However, it doesn't look like key/keyref will actually work. Having tried it (with MSXML 6.0) it appears the selector of a schema key won't accept the document() function in its xpath argument, so we can't examine the item-meta data, whether it appears in an external file or in the schema file itself. It looks like the only place we can look for keys is the instance document.

但是,它看起来不像key / keyref实际上会起作用。尝试过(使用MSXML 6.0),模式键的选择器似乎不接受其xpath参数中的document()函数,因此我们无法检查item-meta数据,无论它是出现在外部文件中还是在架构文件本身。看起来我们可以查找键的唯一地方是实例文档。

So if we really don't want to have a separate list of valid types, we have to do a pre-validation transform, pulling in the item-meta stuff, then do the validation, then do our original transform. That seems overcomplicated for what ought to be a relatively straightforward use of XML schema and stylesheets.

因此,如果我们真的不希望有一个单独的有效类型列表,我们必须进行预验证转换,拉入item-meta的东西,然后进行验证,然后进行原始转换。对于XML架构和样式表的相对简单的使用,这似乎过于复杂。

Is there a better way?

有没有更好的办法?

3 个解决方案

#1


1  

Selectors in key/keyref allow only a very restricted xpath syntax. Short, but not completely accurate: The selector must point to a subnode of the element declared.

key / keyref中的选择器仅允许非常有限的xpath语法。简短但不完全准确:选择器必须指向声明的元素的子节点。

The full definition of the restricted syntax is -> here.

限制语法的完整定义是 - > here。

So, no I don't see a better way, sorry.

所以,不,我没有看到更好的方式,抱歉。

BTW: The W3C states that this restriction was made to make life easier on implementers of XML Schema processors. Keep in mind that one of the design goals of XML Schema was to make it possible to process a document in streaming mode. That explains really a lot of the sometimes seemingly random restrictions of XML Schema.

BTW:W3C声明这种限制是为了让XML Schema处理器的实现者更轻松。请记住,XML Schema的设计目标之一是使以流模式处理文档成为可能。这解释了XML Schema的许多有时看似随机的限制。

#2


0  

Having thought about it a little more, I came up with the idea of having the stylesheet do that part of the validation. The schema would define the item type as a plain string, and the stylesheet would emit a message and stop processing if it couldn't look up the item type in the item-meta table.

考虑到它之后,我想出了让样式表完成验证的那部分。模式将项类型定义为纯字符串,如果样式表无法在item-meta表中查找项类型,则样式表将发出消息并停止处理。

This solution fixes the original problem of having to write down the list of valid types more than once, but it introduces the problem that validation logic is now mixed in with the stylesheet logic. I don't have enough experience with XSD+XSLT to tell whether this new problem is less serious than the old one, but it seems to be more elegant than what I wrote earlier about pulling the item-meta table into each instance document in a pre-validation transform.

这个解决方案修复了不止一次写下有效类型列表的原始问题,但它引入了验证逻辑现在与样式表逻辑混合的问题。我没有足够的经验使用XSD + XSLT来判断这个新问题是否比旧问题更严重,但它似乎比我之前写的关于将item-meta表拉入每个实例文档中的更优雅。预验证转换。

#3


0  

You wouldn't need to stop the XSLT with some error. Just let it produce something that the schema won't validate and that points to the original problem like

您不需要因为某些错误而停止XSLT。只是让它产生架构无法验证的东西,并指出原始问题

 <error txt="Invalid-Item-Type 'invalid_type'"/>

Apart from that please keep in mind that there are no discussion threads here. The posts may go up and down, so it's better to edit your question accordingly.

除此之外,请记住,这里没有讨论主题。帖子可能会上下移动,因此最好相应地编辑您的问题。

Remember, the philosophy here is "One question, and the best answer wins".

请记住,这里的哲学是“一个问题,最好的答案获胜”。

#1


1  

Selectors in key/keyref allow only a very restricted xpath syntax. Short, but not completely accurate: The selector must point to a subnode of the element declared.

key / keyref中的选择器仅允许非常有限的xpath语法。简短但不完全准确:选择器必须指向声明的元素的子节点。

The full definition of the restricted syntax is -> here.

限制语法的完整定义是 - > here。

So, no I don't see a better way, sorry.

所以,不,我没有看到更好的方式,抱歉。

BTW: The W3C states that this restriction was made to make life easier on implementers of XML Schema processors. Keep in mind that one of the design goals of XML Schema was to make it possible to process a document in streaming mode. That explains really a lot of the sometimes seemingly random restrictions of XML Schema.

BTW:W3C声明这种限制是为了让XML Schema处理器的实现者更轻松。请记住,XML Schema的设计目标之一是使以流模式处理文档成为可能。这解释了XML Schema的许多有时看似随机的限制。

#2


0  

Having thought about it a little more, I came up with the idea of having the stylesheet do that part of the validation. The schema would define the item type as a plain string, and the stylesheet would emit a message and stop processing if it couldn't look up the item type in the item-meta table.

考虑到它之后,我想出了让样式表完成验证的那部分。模式将项类型定义为纯字符串,如果样式表无法在item-meta表中查找项类型,则样式表将发出消息并停止处理。

This solution fixes the original problem of having to write down the list of valid types more than once, but it introduces the problem that validation logic is now mixed in with the stylesheet logic. I don't have enough experience with XSD+XSLT to tell whether this new problem is less serious than the old one, but it seems to be more elegant than what I wrote earlier about pulling the item-meta table into each instance document in a pre-validation transform.

这个解决方案修复了不止一次写下有效类型列表的原始问题,但它引入了验证逻辑现在与样式表逻辑混合的问题。我没有足够的经验使用XSD + XSLT来判断这个新问题是否比旧问题更严重,但它似乎比我之前写的关于将item-meta表拉入每个实例文档中的更优雅。预验证转换。

#3


0  

You wouldn't need to stop the XSLT with some error. Just let it produce something that the schema won't validate and that points to the original problem like

您不需要因为某些错误而停止XSLT。只是让它产生架构无法验证的东西,并指出原始问题

 <error txt="Invalid-Item-Type 'invalid_type'"/>

Apart from that please keep in mind that there are no discussion threads here. The posts may go up and down, so it's better to edit your question accordingly.

除此之外,请记住,这里没有讨论主题。帖子可能会上下移动,因此最好相应地编辑您的问题。

Remember, the philosophy here is "One question, and the best answer wins".

请记住,这里的哲学是“一个问题,最好的答案获胜”。