Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

The 'http://www.w3.org/XML/1998/namespace:lang' attribute is not declared Error

1,283 views
Skip to first unread message

Candle

unread,
Apr 17, 2009, 12:00:00 PM4/17/09
to Richard...@hotmail.com
I get the following error when I try to validate XML against a
schema:
The 'http://www.w3.org/XML/1998/namespace:lang' attribute is not
declared

My questions are:
1. Why I am I getting this error?
2. How can I resolve?

I got the schema definition from a third party (ACORD).

I tried to do this using two different methods:

Method 1 – Using a LINQ XDoc (preferred way)

Private Function ValidXsd(ByVal AcordXml As String) As Boolean

Dim schemas As New XmlSchemaSet()
Dim returnVal As Boolean

schemas.Add(Nothing, XmlReader.Create(Server.MapPath("Includes
\acord-pcs-v1_15_1-ns-nodoc-codes.xsd")))

Dim xDocAcordXml = XDocument.Load(New StringReader(AcordXml))

xDocAcordXml.Validate(schemas, AddressOf
ValidationEventHandler, True)

If SchemaValidationResults.Count = 0 Then
returnVal = True
Else
'Populate AcordXml with values in SchemaValidationResults
End If

Return returnVal

End Function

Sub ValidationEventHandler(ByVal sender As Object, ByVal e As
System.Xml.Schema.ValidationEventArgs)

SchemaValidationResults.Add(e.Severity & ": " & e.Message)

End Sub

Method 2 – Using a XmlReader

Private Function ValidXsd2(ByVal AcordXml As String) As Boolean

Dim settings As New XmlReaderSettings()
Dim returnVal As Boolean

settings.Schemas.Add(Nothing, XmlReader.Create(Server.MapPath
("Includes\acord-pcs-v1_15_1-ns-nodoc-codes.xsd")))

settings.ValidationType = ValidationType.Schema
settings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings

AddHandler settings.ValidationEventHandler, AddressOf
settings_ValidationEventHandler

settings.IgnoreWhitespace = True
settings.IgnoreComments = True

Using reader As XmlReader = XmlReader.Create(New StringReader
(AcordXml), settings)
While (reader.Read())
'Empty loop
End While
End Using

If SchemaValidationResults.Count = 0 Then
returnVal = True
Else
'Populate AcordXml with values in SchemaValidationResults
End If

Return returnVal


End Function

' Display any warnings or errors.
Private Sub settings_ValidationEventHandler(ByVal sender As Object,
ByVal e As System.Xml.Schema.ValidationEventArgs)

SchemaValidationResults.Add(e.Severity & ": " & e.Message)

End Sub

I know the above code samples works since I tried them on a simple
example (XML, Schema). Plus I stole them from a book.

Any advice would be much appreciated.

Regards,

R

Martin Honnen

unread,
Apr 17, 2009, 12:16:45 PM4/17/09
to
Candle wrote:
> I get the following error when I try to validate XML against a
> schema:
> The 'http://www.w3.org/XML/1998/namespace:lang' attribute is not
> declared
>
> My questions are:
> 1. Why I am I getting this error?
> 2. How can I resolve?

> Method 2 – Using a XmlReader


>
> Private Function ValidXsd2(ByVal AcordXml As String) As Boolean
>
> Dim settings As New XmlReaderSettings()
> Dim returnVal As Boolean
>
> settings.Schemas.Add(Nothing, XmlReader.Create(Server.MapPath
> ("Includes\acord-pcs-v1_15_1-ns-nodoc-codes.xsd")))
>
> settings.ValidationType = ValidationType.Schema
> settings.ValidationFlags =
> XmlSchemaValidationFlags.ReportValidationWarnings

You can set a flag here to allow xml: attributes like xml:lang or xml:space:
settings.ValidationFlags =
settings.ValidationFlags Or
XmlSchemaValidationFlags.AllowXmlAttributes


Or make your schema import a schema for the namespace
http://www.w3.org/XML/1998/namespace
<xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>
that way I think it should work too.

On the other hand when I test with .NET 3.5 SP1 and try to validate an
XDocument having as follows:

XDocument doc = XDocument.Parse(@"<root xml:lang=""en""
xml:space=""preserve"">whatever</root>");
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(null, @"..\..\XMLSchema1.xsd");
doc.Validate(schemaSet, delegate(object sender,
ValidationEventArgs vargs)
{
Console.WriteLine(vargs.Message);
}, true);
doc.Save(Console.Out);

where the schema is

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="xs:string"/>
</xs:schema>

then I don't get any validation error so I am not sure why you get the
error. Does your schema import a schema for the namespace
http://www.w3.org/XML/1998/namespace which does not define the 'lang'
attribute?

Note that microsoft.public.dotnet.xml is a much better place to discuss
this.

--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/

RC#

unread,
Apr 17, 2009, 5:41:38 PM4/17/09
to
On Apr 17, 12:16 pm, Martin Honnen <mahotr...@yahoo.de> wrote:
> Candle wrote:
> > I get the following error when I try to validate XML against a
> > schema:
> > The 'http://www.w3.org/XML/1998/namespace:lang'attribute is not
> > declared
>
> > My questions are:
> > 1. Why I am I getting this error?
> > 2. How can I resolve?
> > Method 2 – Using a XmlReader
>
> > Private Function ValidXsd2(ByVal AcordXml As String) As Boolean
>
> >         Dim settings As New XmlReaderSettings()
> >         Dim returnVal As Boolean
>
> >         settings.Schemas.Add(Nothing, XmlReader.Create(Server.MapPath
> > ("Includes\acord-pcs-v1_15_1-ns-nodoc-codes.xsd")))
>
> >         settings.ValidationType = ValidationType.Schema
> >         settings.ValidationFlags =
> > XmlSchemaValidationFlags.ReportValidationWarnings
>
> You can set a flag here to allow xml: attributes like xml:lang or xml:space:
>            settings.ValidationFlags =
>              settings.ValidationFlags Or
>                XmlSchemaValidationFlags.AllowXmlAttributes
>
> Or make your schema import a schema for the namespacehttp://www.w3.org/XML/1998/namespace

>    <xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>
> that way I think it should work too.
>
> On the other hand when I test with .NET 3.5 SP1 and try to validate an
> XDocument having as follows:
>
>              XDocument doc = XDocument.Parse(@"<root xml:lang=""en""
> xml:space=""preserve"">whatever</root>");
>              XmlSchemaSet schemaSet = new XmlSchemaSet();
>              schemaSet.Add(null, @"..\..\XMLSchema1.xsd");
>              doc.Validate(schemaSet, delegate(object sender,
> ValidationEventArgs vargs)
>              {
>                  Console.WriteLine(vargs.Message);
>              }, true);
>              doc.Save(Console.Out);
>
> where the schema is
>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
>    <xs:element name="root" type="xs:string"/>
> </xs:schema>
>
> then I don't get any validation error so I am not sure why you get the
> error. Does your schema import a schema for the namespacehttp://www.w3.org/XML/1998/namespacewhich does not define the 'lang'

> attribute?
>
> Note that microsoft.public.dotnet.xml is a much better place to discuss
> this.
>
> --
>
>         Martin Honnen
>        http://msmvps.com/blogs/martin_honnen/- Hide quoted text -
>
> - Show quoted text -

Hi Martin,

I got this to work with you help, however I just want to review what I
did just in case I misunderstood you.

I tried your first suggestion and I got the same error.

Here my code just in case I misunderstood you.

Private Function ValidXsd2(ByVal AcordXml As String) As Boolean

Dim settings As New XmlReaderSettings()
Dim returnVal As Boolean

settings.Schemas.Add(Nothing, XmlReader.Create(Server.MapPath
("Includes\acord-pcs-v1_15_1-ns-nodoc-codes.xsd")))

settings.ValidationType = ValidationType.Schema
settings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings

settings.ValidationFlags = settings.ValidationFlags Or
XmlSchemaValidationFlags.AllowXmlAttributes

AddHandler settings.ValidationEventHandler, AddressOf
settings_ValidationEventHandler

settings.IgnoreWhitespace = True
settings.IgnoreComments = True

Using reader As XmlReader = XmlReader.Create(New StringReader
(AcordXml), settings)
While (reader.Read())
'Empty loop
End While
End Using

If SchemaValidationResults.Count = 0 Then
returnVal = True
Else
'Populate AcordXml with values in SchemaValidationResults
End If

Return returnVal


End Function

I tried your second suggestion.

This line was currently in the schema:


<xsd:import namespace="http://www.w3.org/XML/1998/namespace"

schemaLocation="xml-ns.xsd">

So, I changed it like you suggested to the following:

<xsd:import namespace="http://www.w3.org/XML/1998/namespace">

After I made this change, the processing went into the validation
processing.

Let me know if I misinterpreted any of you suggestions.

Regards

PS Thanks for you help


Martin Honnen

unread,
Apr 18, 2009, 6:06:53 AM4/18/09
to
RC# wrote:

If you get the same error with that suggestion then it seems as if your
schema imports a schema for the namespace
http://www.w3.org/XML/1998/namespace where the imported schema does not
define the 'lang' attribute.

> I tried your second suggestion.
>
> This line was currently in the schema:
> <xsd:import namespace="http://www.w3.org/XML/1998/namespace"
> schemaLocation="xml-ns.xsd">
>
> So, I changed it like you suggested to the following:
>
> <xsd:import namespace="http://www.w3.org/XML/1998/namespace">
>
> After I made this change, the processing went into the validation
> processing.

So that indeed shows that your main schema already imported a schema for
the namespace http://www.w3.org/XML/1998/namespace. That means you got
the error because xml-ns.xsd did not define the 'lang' attribute. Have
you checked the schema xml-ns.xsd? Does that exist in the same location
as the importing schema? Can you post how it looks?

As for the change now get you what you want, yes, if you only do


<xsd:import namespace="http://www.w3.org/XML/1998/namespace">

without providing a schema location then the .NET implementation uses a
default schema for that namespace that defines attributes like xml:lang
or xml:space. So that is one way to get what you want. You could also
remove the xsd:import completely and set the flag AllowXmlAttributes, at
least as long as you use XmlReaderSettings and XmlReader.

RC#

unread,
May 21, 2009, 5:32:46 PM5/21/09
to
> schema imports a schema for the namespacehttp://www.w3.org/XML/1998/namespacewhere the imported schema does not

> define the 'lang' attribute.
>
> > I tried your second suggestion.
>
> > This line was currently in the schema:
> > <xsd:import namespace="http://www.w3.org/XML/1998/namespace"
> > schemaLocation="xml-ns.xsd">
>
> > So, I changed it like you suggested to the following:
>
> > <xsd:import namespace="http://www.w3.org/XML/1998/namespace">
>
> > After I made this change, the processing  went into the validation
> > processing.
>
> So that indeed shows that your main schema already imported a schema for
> the namespacehttp://www.w3.org/XML/1998/namespace. That means you got

> the error because xml-ns.xsd did not define the 'lang' attribute. Have
> you checked the schema xml-ns.xsd? Does that exist in the same location
> as the importing schema? Can you post how it looks?
>
> As for the change now get you what you want, yes, if you only do
>    <xsd:import namespace="http://www.w3.org/XML/1998/namespace">
> without providing a schema location then the .NET implementation uses a
> default schema for that namespace that defines attributes like xml:lang
> or xml:space. So that is one way to get what you want. You could also
> remove the xsd:import completely and set the flag AllowXmlAttributes, at
> least as long as you use XmlReaderSettings and XmlReader.
>
> --
>
>         Martin Honnen
>        http://msmvps.com/blogs/martin_honnen/- Hide quoted text -
>
> - Show quoted text -

Here is code to resolve this issue just in case anyone stumbles on
this post:

(Thanks for your help Martin)

Private SchemaValidationResults As New List(Of String)

Private Function ValidXsd(ByVal AcordXml As String) As Boolean

Dim schemas As New XmlSchemaSet()

Dim returnVal As Boolean

Dim settings As New XmlReaderSettings()

settings.ProhibitDtd = False

schemas.Add(Nothing, XmlReader.Create(Server.MapPath("Includes
\acord-pcs-v1_15_1-ns-nodoc-codes.xsd")))

schemas.Add(Nothing, XmlReader.Create(Server.MapPath("Includes
\xml-ns.xsd"), settings))

Dim xDocAcordXml = XDocument.Load(New StringReader(AcordXml))

xDocAcordXml.Validate(schemas, AddressOf
ValidationEventHandler, True)

If SchemaValidationResults.Count = 0 Then


returnVal = True
Else
'Populate AcordXml with values in SchemaValidationResults

returnVal = False
End If

Return returnVal

End Function

Sub ValidationEventHandler(ByVal sender As Object, ByVal e As

ouss...@gmail.com

unread,
Jun 11, 2019, 6:51:53 AM6/11/19
to

Peter Flynn

unread,
Jul 21, 2019, 2:53:38 PM7/21/19
to
On 11/06/2019 11:51, ouss...@gmail.com wrote:
> Le vendredi 17 avril 2009 16:00:00 UTC, Candle a écrit :
>> I get the following error when I try to validate XML against a
>> schema:
>> The 'http://www.w3.org/XML/1998/namespace:lang' attribute is not
>> declared
>>
>> My questions are:
>> 1. Why I am I getting this error?

The Schema would seem to be defective: it does not contain a declaration
for the "lang" attribute (on some unidentified element).

But that error message looks odd. I don't know why it's mentioning that
URI instead of the one related to the element type where the error
occurred.

>> 2. How can I resolve?

Whatever validator you are using (you don't say) does not identify
*which* element type is in error.

Open the file in an XML editor and click on the Validate button. That
should either jump the cursor to the location of the error, or at least
give you a line number or path to the location.


>> I got the schema definition from a third party (ACORD).

They would be the people to ask.

>> I tried to do this using two different methods:

This looks like a steamroller to crack a nut. Can you just run a
standalone validator that reports properly on the error?

>> I know the above code samples works since I tried them on a simple
>> example (XML, Schema). Plus I stole them from a book.

They may work, but they don't seem to provide any useful information.

Peter

0 new messages