使用 Xpath 在 XSD 模式中选择节点 [英] Selecting Nodes in XSD schema using Xpath

查看:22
本文介绍了使用 Xpath 在 XSD 模式中选择节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,我希望用它来选择按特定顺序需要的所有元素.这是片段:

I have the following code that I wish to use to select all the elements I will need in a certain sequence. Here's the snippet:

            XmlDocument schema = new XmlDocument();
            schema.Load(SchemaFileName);
            XmlNamespaceManager xnm = new XmlNamespaceManager(schema.NameTable);
            xnm.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
            XmlNodeList list = schema.SelectNodes(Path);

但是,我不确定应该写什么作为路径.理想情况下,我想选择sequence"标签的所有子节点,但是当我将 Path 设置为sequence"时,当我运行它时不会给我任何东西.节点列表只是空白.我想要做的是获取验证 xml 文件所需的元素名称(按顺序).

However, I'm not sure what I should write as the path. Ideally I want to select all the child nodes of the "sequence" tag, but when I set the Path to "sequence", that doesn't give me anything when I run it. The nodelist is just blank. What I'm trying to do is get the names of the elements that I will need (in order) for the validation of an xml file.

此外,当我将路径设置为//@name"时,我确实得到了一些东西,但是,它选择了所有具有name"作为属性的元素.我想要的是在sequence"标签之后.

Additionally, when I set the Path as "//@name", I do get something, however, that selects all of the elements with "name" as an attribute. The ones I want are specifically right after the "sequence" tag.

我还尝试将 Path 设置为xs:sequence",但这给了我一个错误:需要命名空间管理器或 XsltContext.此查询具有前缀、变量或用户定义的函数."这很奇怪,因为我以为我已经设置好了..

I've also tried setting the Path as "xs:sequence", but that gives me an error: "Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function." Which is weird because I thought I set it up already..

感谢任何帮助!谢谢!如果您需要更多信息,我很乐意提供.

Any help is appreciated! Thanks! If you need any more information I'll be happy to provide it.

真诚的

tf.rz

我正在使用Visual Studio C# 2008..NET 3.5 SP1

I am using Visual Studio C# 2008. .NET 3.5 SP1

基本前提与我发布的另一个关于数据表列重新排序的问题有关.但要缩短解释.我只需要说我只需要 xsd 模式将验证的元素的名称(按正确的顺序).我有一些 xsd 模式,它们都遵循相同的格式",并且是非常非常静态的文件.因此,我知道我可以安全地查找序列标签并获取其所有子节点.虽然 Michael 提到了编写模式的方法有很多,但我正在使用的模式在这些方面都是相似和静态的,所以如果我能够做到这一点,它将在 100% 的时间内工作.=)

The basic premise is related to another question i have posted regarding the reordering of datatable columns. But to shorten the explanation. I only need to say that I just simply need the names of the elements that the xsd schema will validate (in the proper order). I have a few xsd schemas, all of which follow the same "format" and are very, very static files. Thus, I know that I can safely look for the sequence tag and get all its child nodes. While Michael mentioned how there are many ways to write a schema, the schemas that I am working with are all similar and static in those regards, so if I am able to do this, it will work 100% of the time. =)

推荐答案

您的基本问题是您需要了解如何使用 XPath 访问使用名称空间的源文档.这是非常基本的东西,这个论坛上有一千个答案可以解释它.您需要一个带前缀的名称 - xs:sequence - 并且您需要告诉您的 XPath 引擎xs"前缀代表 URIhttp://www.w3.org/2001/XMLSchema" - 对此的咒语各不相同XPath 引擎到另一个,恐怕我无法识别您代码中的类名,所以我不知道它是哪个.

Your basic problem is that you need to understand how to use XPath to access a source document that uses namespaces. This is pretty elementary stuff and there are a thousand answers on this forum that explain it. You need a prefixed name - xs:sequence - and you need to tell your XPath engine that the "xs" prefix represents the URI "http://www.w3.org/2001/XMLSchema" - the incantation for this varies from one XPath engine to another, and I'm afraid I don't recognize the class names in your code so I don't know which one it is.

我认为还有一个更深层次的问题.使用 XPath 表达式从源 XSD 文档中提取信息有点像尝试使用正则表达式从 Java 程序中提取信息.它有时会起作用;如果你很聪明,你可以让它在很多时候工作;但它永远不会一直有效,因为编写模式的方法太多了.更好的想法是使用真正的模式处理器处理模式,然后使用其 API 询问有关模式内容的问题.

I think there is a deeper problem however. Extracting information from a source XSD document using XPath expressions is a bit like trying to extract information from Java programs using a regular expression. It will work some of the time; if you're clever you can make it work quite a lot of the time; but it will never work all of the time, because there are too many ways of writing a schema. A much better idea is to process the schema using a real schema processor, and then use its API to ask questions about the contents of the schema.

这篇关于使用 Xpath 在 XSD 模式中选择节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆