472,143 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 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 8664
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Adam | last post: by
12 posts views Thread by ~~~ .NET Ed ~~~ | last post: by
4 posts views Thread by tfortney | last post: by
2 posts views Thread by charles-brewster | last post: by
reply views Thread by leo001 | last post: by

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.