Skip to content

Commit 41138b9

Browse files
Auto-identify the XML namespace Issue 22
While the XML namespace may be declared it does not have to be in order to use xml namespaced attributes. This code will automatically recognize xml namespaced attributes, even if the namespace is not explicitly declared.
1 parent 65fc194 commit 41138b9

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/AngleSharp.Xml.Tests/Parser/XmlParsing.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,13 @@ public void ParseInvalidXmlShouldNotThrowWhenSuppressingErrors_Issue14()
146146
parser.ParseDocument(source);
147147
});
148148
}
149+
150+
[Test]
151+
public async Task XmlPrefixedAttributesShouldLocateXmlNamespaceWithoutDeclaration()
152+
{
153+
var document = @"<xml xml:lang=""en""></xml>".ToXmlDocument();
154+
var root = document.DocumentElement;
155+
Assert.AreEqual(NamespaceNames.XmlUri, root.Attributes.Single().NamespaceUri);
156+
}
149157
}
150158
}

src/AngleSharp.Xml/Parser/XmlDomBuilder.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,11 @@ private Attr CreateAttribute(String name, String value)
431431
var prefix = name.Substring(0, colon);
432432
var ns = NamespaceNames.XmlNsUri;
433433

434-
if (!prefix.Is(NamespaceNames.XmlNsPrefix))
434+
if (prefix.Is(NamespaceNames.XmlPrefix))
435+
{
436+
ns = NamespaceNames.XmlUri;
437+
}
438+
else if (!prefix.Is(NamespaceNames.XmlNsPrefix))
435439
{
436440
ns = CurrentNode.LookupNamespaceUri(prefix);
437441
}

0 commit comments

Comments
 (0)