By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

Validation errors in xml file

P: n/a
Hi,
I am trying to use a validation method against this xml and keep
getting 'The 'name' attribute is not declared' and I can't figure out
why.

Here is my XML:

<XmlCommand xmlns="http://intranet/hstServices/Schemas/XmlCommand.xsd"
connectionKey="SomeKey" commandTimeout="60" appId="122"
appScreenType="879">
<Proc name="SomeProcName">
<Param name="@SomeParam" dataType="Char" dataLength="3"
direction="Input">MHR</Param>
</Proc>
<Email>
<Success addresses="SomeEmail" subject="Some Subject"
body="Some Body." />
<Failure addresses="AnotherEmail;AndAnother" subject="Some
Subject." body="SomeBody" />
</Email>
</XmlCommand>

And here is my Schema:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XmlCommand" elementFormDefault="qualified"
version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://intranet/hstServices/Schemas/XmlCommand.xsd"
xmlns="http://intranet/hstServices/Schemas/XmlCommand.xsd"
xmlns:NS="http://intranet/hstServices/Schemas/XmlCommand.xsd">
<xs:attribute name="name" type="xs:string" />
<xs:element name="XmlCommand">
<xs:complexType>
<xs:sequence>
<xs:element name="Proc" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="Param" nillable="true">
<xs:complexType>
<xs:sequence>
<xs:any processContents="skip" />
</xs:sequence>
<xs:attribute ref="name" use="required" />
<xs:attribute name="dataType" use="optional"
type="xs:string" />
<xs:attribute name="dataLength" use="optional"
type="xs:string" />
<xs:attribute name="direction" use="optional"
type="xs:string" />
<xs:attribute name="precision" use="optional"
type="xs:positiveInteger" />
<xs:attribute name="scale" use="optional"
type="xs:positiveInteger" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute ref="name" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="Email" nillable="true">
<xs:complexType>
<xs:sequence>
<xs:element name="Success">
<xs:complexType>
<xs:sequence>
</xs:sequence>
<xs:attribute name="addresses" use="optional"
type="xs:string" />
<xs:attribute name="subject" use="optional"
type="xs:string" />
<xs:attribute name="body" use="optional"
type="xs:string" />
</xs:complexType>
</xs:element>
<xs:element name="Failure">
<xs:complexType>
<xs:sequence>
</xs:sequence>
<xs:attribute name="addresses" use="optional"
type="xs:string" />
<xs:attribute name="subject" use="optional"
type="xs:string" />
<xs:attribute name="body" use="optional"
type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="connectionKey" use="required"
type="xs:string" />
<xs:attribute name="commandTimeout" use ="optional"
type="xs:positiveInteger" />
<xs:attribute name="appId" use="optional"
type="xs:positiveInteger" />
<xs:attribute name="appScreenType" use="optional"
type="xs:positiveInteger" />
</xs:complexType>
</xs:element>
</xs:schema>

and here is my code:

Private Function LoadXml(ByVal xmlCommandString As String) As
XmlDocument
Dim settings As XmlReaderSettings = New
XmlReaderSettings()
settings.Schemas.Add(Nothing,
Server.MapPath(schemaFileName))
settings.ValidationType = ValidationType.Schema
settings.ValidationFlags = settings.ValidationFlags And
XmlSchemaValidationFlags.ReportValidationWarnings
AddHandler settings.ValidationEventHandler, New
ValidationEventHandler(AddressOf ValidationEventHandler)

Dim stringRdr As New StringReader(xmlCommandString)
Dim xmlRdr As XmlReader
xmlRdr = XmlReader.Create(stringRdr, settings)

Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.Load(xmlRdr)

Return xmlDoc
End Function

Private Shared Sub ValidationEventHandler(ByVal sender As Object,
ByVal args As ValidationEventArgs)
Throw New XmlSchemaException(schemaFileValidationErrorMessag e
& args.Message)
End Sub
I think the problem is related to the first instance of this attribute
(above the XMLCommand node) but I don't know where/how to set this
value. It also won't let me remove it without causing issues when I
try to load the xml in the XMLDoc object. Does anyone have any ideas
what is causing this?

<xs:attribute name="name" type="xs:string" />

Apr 20 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a
"Doug" <dn******@dtgnet.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
Hi,
I am trying to use a validation method against this xml and keep
getting 'The 'name' attribute is not declared' and I can't figure out
why.

Here is my XML:

<XmlCommand xmlns="http://intranet/hstServices/Schemas/XmlCommand.xsd"
connectionKey="SomeKey" commandTimeout="60" appId="122"
appScreenType="879">
<Proc name="SomeProcName">
<Param name="@SomeParam" dataType="Char" dataLength="3"
direction="Input">MHR</Param>
</Proc>
<Email>
<Success addresses="SomeEmail" subject="Some Subject"
body="Some Body." />
<Failure addresses="AnotherEmail;AndAnother" subject="Some
Subject." body="SomeBody" />
</Email>
</XmlCommand>

And here is my Schema:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XmlCommand" elementFormDefault="qualified"
version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://intranet/hstServices/Schemas/XmlCommand.xsd"
xmlns="http://intranet/hstServices/Schemas/XmlCommand.xsd"
xmlns:NS="http://intranet/hstServices/Schemas/XmlCommand.xsd">
<xs:attribute name="name" type="xs:string" />
<xs:element name="XmlCommand">
<xs:complexType>
<xs:sequence>
<xs:element name="Proc" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="Param" nillable="true">
<xs:complexType>
<xs:sequence>
<xs:any processContents="skip" />
</xs:sequence>
<xs:attribute ref="name" use="required" />
<xs:attribute name="dataType" use="optional"
type="xs:string" />
<xs:attribute name="dataLength" use="optional"
type="xs:string" />
<xs:attribute name="direction" use="optional"
type="xs:string" />
<xs:attribute name="precision" use="optional"
type="xs:positiveInteger" />
<xs:attribute name="scale" use="optional"
type="xs:positiveInteger" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute ref="name" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="Email" nillable="true">
<xs:complexType>
<xs:sequence>
<xs:element name="Success">
<xs:complexType>
<xs:sequence>
</xs:sequence>
<xs:attribute name="addresses" use="optional"
type="xs:string" />
<xs:attribute name="subject" use="optional"
type="xs:string" />
<xs:attribute name="body" use="optional"
type="xs:string" />
</xs:complexType>
</xs:element>
<xs:element name="Failure">
<xs:complexType>
<xs:sequence>
</xs:sequence>
<xs:attribute name="addresses" use="optional"
type="xs:string" />
<xs:attribute name="subject" use="optional"
type="xs:string" />
<xs:attribute name="body" use="optional"
type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="connectionKey" use="required"
type="xs:string" />
<xs:attribute name="commandTimeout" use ="optional"
type="xs:positiveInteger" />
<xs:attribute name="appId" use="optional"
type="xs:positiveInteger" />
<xs:attribute name="appScreenType" use="optional"
type="xs:positiveInteger" />
</xs:complexType>
</xs:element>
</xs:schema>

and here is my code:
Your code isn't the problem. Your XML is not validated by your schema. Take
a close look at these errors I get from the .NET schema validator as run by
Stylus Studio:
--------
Running custom validation engine .NET XML Parser...

Doug-2007-04-20.xml:6,11: The 'name' attribute is not declared.
Doug-2007-04-20.xml:6,6: The required attribute
'http://intranet/hstServices/Schemas/XmlCommand.xsd:name' is missing.
Doug-2007-04-20.xml:7,16: The 'name' attribute is not declared.
Doug-2007-04-20.xml:7,10: The required attribute
'http://intranet/hstServices/Schemas/XmlCommand.xsd:name' is missing.
Doug-2007-04-20.xml:7,83: The element 'Param' in namespace
'http://intranet/hstServices/Schemas/XmlCommand.xsd' cannot contain text.
List of possible elements expected: '##any:*'.
Doug-2007-04-20.xml:7,88: The element 'Param' in namespace
'http://intranet/hstServices/Schemas/XmlCommand.xsd' has incomplete content.
List of possible elements expected: '##any:*'.

-----------------------------------
Validation complete with 6 error(s)

You'll need to qualify your references to the global name attribute. Try
defining a namespace prefix for it in your XML file, then use it to refer to
name:
<XmlCommand xmlns="http://intranet/hstServices/Schemas/XmlCommand.xsd"
connectionKey="SomeKey"
xmlns:tns="http://intranet/hstServices/Schemas/XmlCommand.xsd"
commandTimeout="60" appId="122" appScreenType="879" >
<Proc tns:name="SomeProcName">
<Param tns:name="@SomeParam" dataType="Char" dataLength="3"
direction="Input">MHR</Param>
</Proc>
<Email>
<Success addresses="SomeEmail" subject="Some Subject" body="Some
Body."/>
<Failure addresses="AnotherEmail;AndAnother" subject="Some Subject."
body="SomeBody"/>
</Email>
</XmlCommand>
--

John Saunders [MVP]
Apr 21 '07 #2

P: n/a
You'll need to qualify your references to the global name attribute.

I'm not sure what you mean by this when you say qualify your reference
to this attribute. But I tried adding the xmlns:tns entry you have in
the example below and it still failed.
Apr 21 '07 #3

P: n/a
Doug wrote:
I'm not sure what you mean by this when you say qualify your reference
to this attribute. But I tried adding the xmlns:tns entry you have in
the example below and it still failed.
What exactly do you want to define? If you want to define that the
attribute named 'name' is in the target namespace of your schema then
within the XML sample you need to qualify the attribute as e.g.
<Proc xmlns:pf="http://intranet/hstServices/Schemas/XmlCommand.xsd"
xmlns="http://intranet/hstServices/Schemas/XmlCommand.xsd"
pf:name="SomeProcName">

If that name attribute is supposed to be in no namespace then you can't
define it as a global attribute in a schema for the target namespace
http://intranet/hstServices/Schemas/XmlCommand.xsd. You would need to
define a schema for no namespace and define the attribute in there, then
you need to import the schema and reference the attribute. Or you need
to inline the attribute definition as you have done for the other
attributes, that way they are unqualified.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 21 '07 #4

P: n/a
Hello,

I'll be honest, I'm not sure exactly what you are saying either.
Here's my dilemma: The schema was defined by someone else and I
enhanced it by adding things like the email node etc. The global
"<xs:attribute name="name" type="xs:string" />" that exists above the
XmlCommand node was put there by the person who first wrote the schema
(it is not a production schema yet so I am able to modify it).
However, if I try to remove that line from the schema, then when the
code tries to load the xml into the XMLDoc object, it bombs. So It's
like I'm stuck having to have it, but I don't know how to use it (and
the person who first built this schema is not currently available to
help me with this.

So are you saying that I have to have something like this -
essentially putting those two references to the namespace in every
node that has a name attribute (or in all nodes period)? If so, that
sure seems excessive. Is there some way I can just remove that global
attribute altogether?

<XmlCommand xmlns:pf="http://intranet/hstServices/Schemas/
XmlCommand.xsd" xmlns="http://intranet/hstServices/Schemas/
XmlCommand.xsd" connectionKey="SomeKey" commandTimeout="60"
appId="122"
appScreenType="879">
<Proc xmlns:pf="http://intranet/hstServices/Schemas/XmlCommand.xsd"
xmlns="http://intranet/hstServices/Schemas/
XmlCommand.xsd"name="SomeProcName">
<Param xmlns:pf="http://intranet/hstServices/Schemas/
XmlCommand.xsd" xmlns="http://intranet/hstServices/Schemas/
XmlCommand.xsd" name="@SomeParam" dataType="Char" dataLength="3"
direction="Input">MHR</Param>
</Proc>
<Email>
<Success addresses="SomeEmail" subject="Some Subject"
body="Some Body." />
<Failure addresses="AnotherEmail;AndAnother" subject="Some
Subject." body="SomeBody" />
</Email>
</XmlCommand>

Apr 21 '07 #5

P: n/a
Hello again,

Okay, I figured out how to get the first error to go away and how to
remove the global attribute (I couldn't delete it before because the
other nodes were referencing it.

Now I get this error - which John also got in his response:

The element 'Param' in namespace 'http://intranet/hstServices/Schemas/
XmlCommand.xsd' cannot contain text. List of possible elements
expected: any element in namespace '##any'.

What does that mean? I can't have a node that is like this?

<Param>SomeValue</Param>

Apr 21 '07 #6

P: n/a
Doug wrote:
Now I get this error - which John also got in his response:

The element 'Param' in namespace 'http://intranet/hstServices/Schemas/
XmlCommand.xsd' cannot contain text. List of possible elements
expected: any element in namespace '##any'.

What does that mean? I can't have a node that is like this?

<Param>SomeValue</Param>
Your schema specifies that the element has other elements (xs:any) as
its contents. If you do not want element contents but rather text
content then you need e.g.

<xs:element name="Param" nillable="true">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" use="required"
type="xs:string/>
<xs:attribute name="dataType" use="optional"
type="xs:string" />
<xs:attribute name="dataLength" use="optional"
type="xs:string" />
<xs:attribute name="direction" use="optional"
type="xs:string" />
<xs:attribute name="precision" use="optional"
type="xs:positiveInteger" />
<xs:attribute name="scale" use="optional"
type="xs:positiveInteger" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>

That way the element has simple contents (in this case xs:string but you
could of course use other simple types like xs:int) and has attributes.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 22 '07 #7

P: n/a
I tried that and got this error now:

Schema validation error occurred: The element 'Proc' in namespace
'http://intranet/hstServices/Schemas/XmlCommand.xsd' has invalid child
element 'Param' in namespace 'http://intranet/hstServices/Schemas/
XmlCommand.xsd'.

Apr 23 '07 #8

P: n/a
Doug wrote:
Schema validation error occurred: The element 'Proc' in namespace
'http://intranet/hstServices/Schemas/XmlCommand.xsd' has invalid child
element 'Param' in namespace 'http://intranet/hstServices/Schemas/
XmlCommand.xsd'.
Show us the schema and XML document you have.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 23 '07 #9

P: n/a
Here is my schema:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XmlCommand" elementFormDefault="qualified"
version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://intranet/hstServices/Schemas/XmlCommand.xsd"
xmlns="http://intranet/hstServices/Schemas/XmlCommand.xsd"
xmlns:NS="http://intranet/hstServices/Schemas/XmlCommand.xsd">
<xs:element name="XmlCommand">
<xs:complexType>
<xs:sequence>
<xs:element name="Proc" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="Param" nillable="true">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" use="required"
type="xs:string"/>
<xs:attribute name="dataType" use="optional"
type="xs:string" />
<xs:attribute name="dataLength" use="optional"
type="xs:string" />
<xs:attribute name="direction" use="optional"
type="xs:string" />
<xs:attribute name="precision" use="optional"
type="xs:positiveInteger" />
<xs:attribute name="scale" use="optional"
type="xs:positiveInteger" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="Email" nillable="true">
<xs:complexType>
<xs:sequence>
<xs:element name="Success">
<xs:complexType>
<xs:sequence>
</xs:sequence>
<xs:attribute name="addresses" use="optional"
type="xs:string" />
<xs:attribute name="subject" use="optional"
type="xs:string" />
<xs:attribute name="body" use="optional"
type="xs:string" />
</xs:complexType>
</xs:element>
<xs:element name="Failure">
<xs:complexType>
<xs:sequence>
</xs:sequence>
<xs:attribute name="addresses" use="optional"
type="xs:string" />
<xs:attribute name="subject" use="optional"
type="xs:string" />
<xs:attribute name="body" use="optional"
type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="connectionKey" use="required"
type="xs:string" />
<xs:attribute name="commandTimeout" use ="optional"
type="xs:positiveInteger" />
<xs:attribute name="appId" use="optional"
type="xs:positiveInteger" />
<xs:attribute name="appScreenType" use="optional"
type="xs:positiveInteger" />
</xs:complexType>
</xs:element>
</xs:schema>

and my XML hasn't changed:

<XmlCommand xmlns="http://intranet/hstServices/Schemas/
XmlCommand.xsd"
connectionKey="SomeKey" commandTimeout="60" appId="122"
appScreenType="879">
<Proc name="SomeProcName">
<Param name="@SomeParam" dataType="Char" dataLength="3"
direction="Input">MHR</Param>
</Proc>
<Email>
<Success addresses="SomeEmail" subject="Some Subject"
body="Some Body." />
<Failure addresses="AnotherEmail;AndAnother" subject="Some
Subject." body="SomeBody" />
</Email>
</XmlCommand>


Apr 23 '07 #10

P: n/a
Doug wrote:
Here is my schema:
[snip]
and my XML hasn't changed:
[snip]

That XML validates just fine against the schema with Visual Studio 2005.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 23 '07 #11

P: n/a
That XML validates just fine against the schema with Visual Studio 2005.

I don't get it then. I am using the same xml, the same schema and
the .NET code that I produced earlier and I am getting the error I
described. At this point, I'm ready to scrap the validation method
altogether because it is more trouble than it's worth.

Apr 23 '07 #12

P: n/a
I found out why it won't validate but not what to do about it. The
problem is (and I apologize for not showing you this in my example) is
that I need multiple Param rows as such. The moment I do that, I get
that error I mentioned previously (if I have only one row, it
validates fine). How do I update the schema so I can have multiple
rows?
<XmlCommand xmlns="http://intranet/hstServices/Schemas/XmlCommand.xsd"
connectionKey="SomeKey" commandTimeout="60" appId="122"
appScreenType="879">
<Proc name="SomeProcName">
<Param name="@SomeParam" dataType="Char" dataLength="3"
direction="Input">MHR</Param>
<Param name="@SomeOtherParam" dataType="Char" dataLength="3"
direction="Input">ABC</Param>
</Proc>
<Email>
<Success addresses="SomeEmail" subject="Some Subject"
body="Some Body." />
<Failure addresses="AnotherEmail;AndAnother" subject="Some
Subject." body="SomeBody" />
</Email>
</XmlCommand>

Apr 23 '07 #13

P: n/a
Doug wrote:
I found out why it won't validate but not what to do about it. The
problem is (and I apologize for not showing you this in my example) is
that I need multiple Param rows as such. The moment I do that, I get
that error I mentioned previously (if I have only one row, it
validates fine). How do I update the schema so I can have multiple
rows?
Change
<xs:element name="Proc" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="Param" nillable="true">

to

<xs:element name="Proc" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="Param" nillable="true"
maxOccurs="unbounded">

(or if you want to allow e.g. a maximum of ten such Param elements then
obviously e.g.

<xs:element name="Proc" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="Param" nillable="true" maxOccurs="10">
)
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 23 '07 #14

P: n/a
That did it! Thank you!
Apr 23 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.