简体   繁体   English

我可以根据 XML 模式验证 XPath 表达式吗?

[英]Can I validate an XPath expression against an XML schema?

You can verify an XPath expression against an XML doc to verify it, but is there an easy way to verify the same XPath expression against the schema for that document?您可以根据 XML 文档验证 XPath 表达式来验证它,但是是否有一种简单的方法可以根据该文档的架构验证相同的 XPath 表达式?

Say I have an XSD schema like this:假设我有一个像这样的 XSD 架构:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" ... etc>
  <xsd:element name="RootData">
    <xsd:complexType>
      <xsd:sequence minOccurs="0">
        <xsd:element name="FirstChild">
          <xsd:complexType>
            <xsd:sequence minOccurs="0">
              <xsd:element name="FirstGrandChild">
... etc etc

Is there an easy or built-in way to verify that the XPath:是否有一种简单或内置的方法来验证 XPath:

/RootData/FirstChild/FirstGrandChild

would be valid against any XML documents that may be based on that schema?对可能基于该模式的任何 XML 文档是否有效? ( Edit : I guess I mean potentially valid ; the actual XML document might not contain those elements, but that XPath could still be considered potentially valid for the schema. Whereas, say, /RootData/ClearlyInvalidChild/ThisElementDoesntExistEither is clearly invalid.) 编辑:我想我的意思是可能有效实际的 XML 文档可能不包含这些元素,但是 XPath 仍然可以被认为对架构可能有效。而例如/RootData/ClearlyInvalidChild/ThisElementDoesntExistEither显然是无效的。)

Of course I could only expect this to work against canonical XPath expressions rather than ones of arbitrary complexity, but that's fine.当然,我只能期望这适用于规范的 XPath 表达式,而不是任意复杂的表达式,但这很好。

I'm specifically thinking in .NET but am curious if other implementations make it possible.我特别想在 .NET 但很好奇其他实现是否可以实现。 It's not so important that I want to roll my own, for example I don't really want to write my own code to transform that XPath expression into another one like:我想自己动手并不那么重要,例如,我真的不想编写自己的代码来将 XPath 表达式转换为另一个表达式,例如:

/xsd:schema/xsd:element[@name='RootData']/xsd:complexType/xsd:sequence/xsd:element[@name='FirstChild']/...etc...

... though I know I could do that if I really had to. ......虽然我知道如果我真的必须这样做,我可以这样做。

Cheers!干杯!

We actually did a research project on this, and implemented an XPath verifier, sometime around 2000. This was for XPath 1. I am not aware of any currently available libraries that you can use to do this.实际上,我们对此进行了一个研究项目,并在 2000 年左右的某个时候实施了 XPath 验证程序。这是针对 XPath 1 的。我不知道您可以使用任何当前可用的库来执行此操作。

If you want to go and implement this yourself, here are some hints:如果你想 go 并自己实现,这里有一些提示:

  • You will not be able to transform a path over an instance document into a path over a schema as you do above.您将无法像上面那样将实例文档上的路径转换为架构上的路径。 For example, /a//b does not transform into /xsd:element[@name='a']//xsd:element[@name='b'] because element b may be defined at the top level of the schema, not underneath b.例如, /a//b不会转换为/xsd:element[@name='a']//xsd:element[@name='b']因为元素 b 可能定义在架构的顶层,不在 b 之下。

  • Remember that while an XML document is a tree, a schema is a graph.请记住,虽然 XML 文档是树,但模式是图。 If you search descendant paths like //a, you will have to decide when to terminate the search or it may continue forever (eg imagine in a element "a" that contains "b", which contains "a")如果你搜索像 //a 这样的后代路径,你将不得不决定何时终止搜索,否则它可能会永远继续下去(例如,想象一个包含“b”的元素“a”,其中包含“a”)

  • Some paths will be undecidable or at least very hard to decide.有些路径将无法确定或至少很难确定。 For example //*[starts-with(@name, 'foo')]例如//*[starts-with(@name, 'foo')]

If you're still up for it, I suggest using a library like eclipse's XSD or the .NET schema loading classes to load the schema into memory and do your checking in code.如果您仍然愿意,我建议使用 eclipse 的XSD或 .NET 模式加载类之类的库将模式加载到 memory 中并检查代码。

At design time, you could use a tool to generate a sample XML document and execute your XPath against the sample.在设计时,您可以使用工具生成示例 XML 文档并针对示例执行 XPath。 Altova XML Spy has this feature, as does SOAP UI. Altova XML Spy 具有此功能,SOAP UI 也是如此。

SOAP UI is actually open source (Java) so maybe you can take a peek and see how it generates the samples. SOAP UI 实际上是开源的 (Java),所以也许你可以看看它是如何生成示例的。 In a runtime situation (ie if schema and XPath are both inputs to a running program) then you'd have to ensure enough optional components and sample data was generated to avoid false negatives and may need to generate multiple example files.在运行时情况下(即如果模式和 XPath 都是运行程序的输入),那么您必须确保生成了足够的可选组件和示例数据以避免误报,并且可能需要生成多个示例文件。

I wouldn't try to evaluate the XPath against the schema directly as the various Axes would make a complete solution very complicated.我不会尝试直接针对架构评估 XPath,因为各种轴会使完整的解决方案变得非常复杂。 I'm pretty sure that could be done, but it strikes me as hard core mathematics.我很确定这是可以做到的,但它给我的印象是核心数学。 I propose generating samples as a short-cut.我建议生成样本作为捷径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM