Selecting Nodes in XSD schema using Xpath
Asked Answered
S

1

1

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

Sidewinder answered 29/6, 2011 at 16:38 Comment(0)
H
2

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.

Hence answered 30/6, 2011 at 9:12 Comment(1)
Thanks a lot, in theory that makes a lot more sense now, but I do agree that working with namespaces is the main problem here. I've extracted the node list easily in other xml documents without trouble. I've added a bit of information in my post to help you understand why I'm doing what I'm doing. This concept of using a schema processor intrigues me, I will do a bit of research on that as well. Is there a name for this process or is it simply "use API to obtain schema contents"? My apologies, I'm fairly new to programming.Sidewinder

© 2022 - 2024 — McMap. All rights reserved.