473,390 Members | 1,229 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

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 8728
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.