使用 Xpath 选择 XSD 模式中的节点

发布于 2024-11-17 21:21:13 字数 1024 浏览 3 评论 0原文

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

            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);

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

此外,当我将路径设置为“//@name”时,我确实得到了一些东西,但是,它选择了所有以“name”作为属性的元素。我想要的那些就在“序列”标签之后。

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

任何帮助都是值得赞赏的!谢谢!如果您需要更多信息,我将很乐意提供。

此致,

tf.rz

编辑:我正在使用 Visual Studio C# 2008。 .NET 3.5 SP1

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

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);

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.

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.

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.

Sincerely,

tf.rz

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

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. =)

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

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

发布评论

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

评论(1

〃温暖了心ぐ 2024-11-24 21:21:13

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

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

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.

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.

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