473,467 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

XML ID attribute & GetElementById

>Is it actually valid to have an XML document with 2 XML Elements having the
same ID attribute? XML Spy seems to allow something like the following
<?xml version="1.0" encoding="UTF-8"?>
<FormDefinition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Forms.xsd">
<list id="1">
<status>partially completed</status>
</list>
<list id="1">
<status>completed</status>
</list>
</FormDefinition>

If it is valid, how can I utilise System.Xml.XmlDocument.GetElementById
method() (which returns 1 XmlNode)?
Nov 12 '05 #1
3 8731
With the following XSD:
<!---------------------------Start of Form.xsd--------------------------->
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="FormDefinition">
<xs:complexType>
<xs:sequence>
<xs:element name="FormName" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>

</xs:element>
<xs:element name="FormTitle" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="ReadFromDB" type="xs:boolean" use="required"/>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
<!---------------------------End of Form.xsd--------------------------->

And the following XML Document
<!---------------------------Start of Test.xml--------------------------->
<?xml version="1.0" encoding="UTF-8"?>
<FormDefinition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Form.xsd">
<FormName id="a1">test form </FormName>
<FormTitle ReadFromDB="true" id="a2">Island</FormTitle>
</FormDefinition>
<!---------------------------End of Test.xml--------------------------->

xmlDoc1.GetElementById("a1") is returning null. How can i fix this? Can I
have an xs:ID that contains numbers only? Is there a method to search for a
node given an id attribute instead?

"Patrick" wrote:
Is it actually valid to have an XML document with 2 XML Elements having the

same ID attribute? XML Spy seems to allow something like the following
<?xml version="1.0" encoding="UTF-8"?>
<FormDefinition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Forms.xsd">
<list id="1">
<status>partially completed</status>
</list>
<list id="1">
<status>completed</status>
</list>
</FormDefinition>

If it is valid, how can I utilise System.Xml.XmlDocument.GetElementById
method() (which returns 1 XmlNode)?

Nov 12 '05 #2
Currently XmlDocument.GetElementById() works only
with the ids specified through a DTD. The XSD info is
ignored for this scenario. If you know the schema ahead
of time you might want to simply use SelectNodes() a la:
XmlDocument.SelectNodes("//FormName[@id='a1']")

Ion

"Patrick" <qu*******@newsgroup.nospam> wrote in message
news:6E**********************************@microsof t.com...
With the following XSD:
<!---------------------------Start of Form.xsd--------------------------->
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="FormDefinition">
<xs:complexType>
<xs:sequence>
<xs:element name="FormName" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>

</xs:element>
<xs:element name="FormTitle" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="ReadFromDB" type="xs:boolean" use="required"/>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
<!---------------------------End of Form.xsd--------------------------->

And the following XML Document
<!---------------------------Start of Test.xml--------------------------->
<?xml version="1.0" encoding="UTF-8"?>
<FormDefinition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Form.xsd">
<FormName id="a1">test form </FormName>
<FormTitle ReadFromDB="true" id="a2">Island</FormTitle>
</FormDefinition>
<!---------------------------End of Test.xml--------------------------->

xmlDoc1.GetElementById("a1") is returning null. How can i fix this? Can I have an xs:ID that contains numbers only? Is there a method to search for a node given an id attribute instead?

"Patrick" wrote:
Is it actually valid to have an XML document with 2 XML Elements having
the same ID attribute? XML Spy seems to allow something like the following
<?xml version="1.0" encoding="UTF-8"?>
<FormDefinition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Forms.xsd">
<list id="1">
<status>partially completed</status>
</list>
<list id="1">
<status>completed</status>
</list>
</FormDefinition>

If it is valid, how can I utilise System.Xml.XmlDocument.GetElementById
method() (which returns 1 XmlNode)?

Nov 12 '05 #3
Patrick wrote:
Can I
have an xs:ID that contains numbers only?
Not, in XML ID cannot be numbers only.
Is there a method to search for a
node given an id attribute instead?


Sure. Use XPath:

xmlDoc.SelectNodes("//*[@id='1']");

--
Oleg Tkachenko [XML MVP, MCAD]
http://blog.tkachenko.com
Nov 12 '05 #4

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

Similar topics

12
by: Sammy | last post by:
Hi, my mind is going crazy. I have tried everything I can think of to no avail. I have tried Disable Output Escaping. I tried to think of a way of enclosing the attribute data in a CDATA...
2
by: Adam | last post by:
I have a scrollbar that runs at the top of my page, I have it in a external js file and call it in the head tags of the webpage code. can you one tell me how to get this code to work with other...
4
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
12
by: ~~~ .NET Ed ~~~ | last post by:
Hi, I have a standalone XML file (with the appropriate xml document header) that works fine when I load it using XmlDocument. I can have child elements like this without problems: ...
4
by: tfortney | last post by:
I created an iframe HtmlGenericControl that has an ampersand in the "src" attribute..... Dim faxFrame As New HtmlGenericControl("iframe") faxFrame.Attributes.Add("width", "100%")...
2
by: charles-brewster | last post by:
I'm trying to write a simple JavaScript function which will use a button to copy table cell data into a form input text box as the "value" attribute. The following is intended to test the...
7
chunk1978
by: chunk1978 | last post by:
can someone please tell me what is wrong with this function's syntax? it's not working for me... if (document.getElementById('DIVinvoicesubtotal').style.display = 'none' &&...
15
by: Inny | last post by:
Hello, I found this simple js star rating script that I want to modify slightly. firstly I want to retain current vote , say 3 stars, untill its changed again. right now it resets to unvoted...
5
by: elbin | last post by:
Hello, first to say that I am a total beginner in Javascript but I know some programming (python in particular) and am able to understand methods/parameters and so on. Here's my problem: I am...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
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,...
0
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...
0
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...
0
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 ...

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.