473,655 Members | 3,056 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determining if an Attribute is Allowed by the Schema

I'm using an XmlValidatingRe ader to read an XML document and parse it against
my schema. While I'm reading each node, I need to determine if the schema
allows an ID attribute for the node. The ID is optional so it may not exist
in the XML document. Is there a way to determine if the attribute is allowed
or not? I can't seem to find any information on this anywhere.
Nov 12 '05 #1
2 1625
Hi J

You use the classes in the System.Xml.Sche ma namespace to do this. Here's
some (inefficient!) code that shows how to read through a document, and test
whether the element can have an attribute called "id"...

HTH

Nigel Armstrong

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim t As New System.Xml.XmlT extReader("C:\t esta.xml")
Dim v As New System.Xml.XmlV alidatingReader (t)
v.ValidationTyp e = Xml.ValidationT ype.Schema
AddHandler v.ValidationEve ntHandler, AddressOf VEH
Dim som As System.Xml.Sche ma.XmlSchema =
System.Xml.Sche ma.XmlSchema.Re ad(New IO.FileStream(" C:\test.xsd",
IO.FileMode.Ope n), AddressOf VEH)
som.Compile(Add ressOf VEH)

While v.Read
If v.NodeType = Xml.XmlNodeType .Element Then
Dim oc As System.Xml.Sche ma.XmlSchemaObj ectCollection =
som.Items
For Each o As Object In oc
If TypeOf o Is System.Xml.Sche ma.XmlSchemaEle ment Then
If o.Name = v.Name Then
Dim ct As System.Xml.Sche ma.XmlSchemaCom plexType
= o.SchemaType
For Each a As
System.Xml.Sche ma.XmlSchemaAtt ribute In ct.Attributes
If a.Name = "id" Then
MessageBox.Show (v.Name & " has an id
attribute")
End If
Next
End If
End If
Next
End If
End While
v.Close()
End Sub

Private Sub VEH(ByVal sender As Object, ByVal e As
System.Xml.Sche ma.ValidationEv entArgs)
Debug.WriteLine (e.Message)
End Sub

"JMonnin" wrote:
I'm using an XmlValidatingRe ader to read an XML document and parse it against
my schema. While I'm reading each node, I need to determine if the schema
allows an ID attribute for the node. The ID is optional so it may not exist
in the XML document. Is there a way to determine if the attribute is allowed
or not? I can't seem to find any information on this anywhere.

Nov 12 '05 #2
Let's try that again...

This is a little better - but it does rely on the fact that all the elements
in my particular schema are complex types...

HTH more!

Nigel Armstrong

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim t As New System.Xml.XmlT extReader("C:\t esta.xml")
Dim v As New System.Xml.XmlV alidatingReader (t)
v.ValidationTyp e = Xml.ValidationT ype.Schema
AddHandler v.ValidationEve ntHandler, AddressOf VEH

While v.Read
If v.NodeType = Xml.XmlNodeType .Element Then
Dim ct As System.Xml.Sche ma.XmlSchemaCom plexType =
CType(v.SchemaT ype, System.Xml.Sche ma.XmlSchemaCom plexType)
For Each a As System.Xml.Sche ma.XmlSchemaAtt ribute In
ct.Attributes
If a.Name = "id" Then
MessageBox.Show (v.Name & " has an id attribute")
End If
Next
End If
End While
v.Close()
End Sub

Private Sub VEH(ByVal sender As Object, ByVal e As
System.Xml.Sche ma.ValidationEv entArgs)
Debug.WriteLine (e.Message)
End Sub

"JMonnin" wrote:
I'm using an XmlValidatingRe ader to read an XML document and parse it against
my schema. While I'm reading each node, I need to determine if the schema
allows an ID attribute for the node. The ID is optional so it may not exist
in the XML document. Is there a way to determine if the attribute is allowed
or not? I can't seem to find any information on this anywhere.

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1602
by: Simon Vaillancourt | last post by:
I've tried to find how to write a schema for this type of node everywhere. <Param name="FirstParam">ABCD</Param> How do i arrange a complex type that contains an attribute and a text. This : <xs:complexType name="Param"> <xs:attribute name="name" /> </xs:complexType>
0
1418
by: Carl | last post by:
I want to create a generic xslt that would take xml input like: ########################################################################## <?xml version="1.0" encoding="utf-8" ?> <?xml-stylesheet type="text/xsl" href="lookup.xslt"?> <root> <Schema name="Schema1" xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes"> <ElementType name="lookup" content="empty" model="closed"> <AttributeType name="col1"...
0
1488
by: Ingrid | last post by:
Am I right in thinking that datatyping at element level ie <xs:element name="num" type="xs:integer"> and specifying a choice of attribute values ie <xs:attribute name="kind"> <xs:simpleType> <xs:restriction base="xs:token"> <xs:enumeration value="volume_number"/> <xs:enumeration value="page_range"/>
4
2932
by: Lénaïc Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello, I've some namespace problems when defining default values for attributes. My problem seems to come from the fact that the attributes are defined in a different namespace from the element.
4
22109
by: matt hegarty | last post by:
Hi I am relatively new to XML, but am having problems with a validation issue. When the XML is parsed and validated using Xerces, the following error is seen: cvc-complex-type.3.2.2: Attribute 'xsi:noNamespaceSchemaLocation' is not allowed to appear in element 'questionFeed'.
2
2156
by: Dirk Declercq | last post by:
Hi, Is it possible in Xml to enfore the uniqueness of an element based on his attribute value. Say I have this schema : <?xml version="1.0" encoding="UTF-8"?> <xs:schema targetNamespace="http://www.egemin.com/Epia/StringResources" xmlns:xs="http://www.w3.org/2001/XMLSchema"
7
11938
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return "DirectoryEntry" objects within the path that I specify (either using the DirectoryEnrtry.Children or using the DirectorySearcher class) and all started well and dandy! Now the problem; some of the properties of the DirectoryEntry objects being...
0
1648
by: Aray | last post by:
Let us see the test file following: -------file nowork.xsd begin----------- <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.bonc.com.cn" targetNamespace="http://www.bonc.com.cn" elementFormDefault="qualified"> <xs:attribute name="testAttribute" type="xs:string"/> <xs:attributeGroup name="testAttributeGroup"> <xs:attribute ref="testAttribute"/>
4
2701
by: Eric | last post by:
Attached is an example of my question. Note the "values" attribute is optional. Also the <valuesub-element is optional. Here, the XML can contain, 1 or both or neither. I would like to allow EITHER the "values" attribute or <valuesub-elements - but not both. I tried using a <choicewith two element definitions for "characteristic" with different content, but this is clearly not allowed in XSD. Does anyone have an answer? Thanks.
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.