English 中文(简体)
在科学、社会、文化权利委员会,我能否根据父母的内容确定强制性的儿童内容?
原标题:In XSD, can I set mandatory child elements based on parent element s content?
  • 时间:2012-01-15 08:11:36
  •  标签:
  • xml
  • xsd

我有一个XML &XSD的政变,其中包括类似于以下内容的房屋:

<root>
...
<House>
    <id>1</id>
    <type>Cottage</type>
    <imageSrc> image.jpg </imageSrc>
</House>
<House>
    <id>2</id>
    <type>Private</type>
</House>    

我想通过我的社发司强制执行,所有类型的“部落”都必须有强制性的标签,但其他房屋却没有。 (另一种方式是:唯一的私人公司而不是<>em>。) 是否有办法这样做?

问题回答

您不妨考虑使用Schematron等内容来表达这些超出XML Schema能力的额外验证规则。 Schematron是开放式(ISO)标准,允许你就XML文件中应当存在的模式表示意见。 例如,你可以利用它来表达制约因素,例如:

  • if element A is present, element B is prohibited. If A and B are siblings, you could do this using an xs:choice in XML schema, but this approach doesn t work in XML Schema if the two elements aren t siblings.
  • if element A has an attribute with a specific value, then it must contain child element B.
  • element A must be equal to the total of elements B and C.

在你的具体例子中,我会假设,<代码>imageSrc是强制性的标签,你想要在家庭之家任职,并对他人加以禁止。

一个简单的Schematron schema来表达这些说法的例子就是:

<schema xmlns="http://purl.oclc.org/dsdl/schematron">
  <pattern>
    <!-- Houses of type  Cottage  must contain an imageSrc element -->
    <rule context = "/root/House[type/text() =  Cottage ]">
      <assert test = "imageSrc">Cottages must have an image</assert>
    </rule>

    <!-- Houses not of type  Cottage  must not contain an imageSrc element -->
    <rule context = "/root/House[type/text() !=  Cottage ]">
      <assert test = "not(imageSrc)">Non-cottages must not have an image</assert>
    </rule>
  </pattern>
</schema>

我在此表示,这种模式是两项单独的规则,是你要求的两个部分中的每一个部分。

每一条<代码>>>>>>> >条/代码>都有一个<代码>context属性,即XPath表述。 当这一表述真实时,规则火力就会消失。 因此,在<代码>House <>/code>中带有<条码>类型的儿童部分的第1条规则火灾,其文本内容等同于家庭。

When the rule fires, the assertion is then checked, by evaluating the test attribute of the assert element. Again, this is an XPath expression, but the context node for evaluating the test expression is the node matched by the rule element (in this case House). When the test expression evaluates to true, the assertion passes (i.e. the document is valid with respect to this assertion). If it evaluates to false, the assertion fails. The text content of the assert element is typically a human-readable description of the assertion that could be passed onto end users. Schematron also offers additional facilities (called diagnostics ) to link additional technical messages to assertions.

可在上查阅。 典型的验证管道将首先对XML Schema文件进行验证,该文件界定了该文件的基本结构要求(例如要素等级、心脏、数据类型、模式等)。 然后,一旦文件达到这一基本验证水平,你就会对Schematron schema的文件进行验证,以证实基于模式的其他限制。 这一管道可按方案实施,或(例如)使用批量文档、字母或XProc处理器。

虽然科学、科学和技术司确实不支持这种行为,但你可以简单地界定两种类型的住房,一种是强制性的,另一种是没有。





相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml