From 41138b92bf1ba9f064c26b4f0578c9e9e07aa3cd Mon Sep 17 00:00:00 2001 From: "jason.bray@faithlife.com" Date: Tue, 18 Apr 2023 14:06:53 -0400 Subject: [PATCH] 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. --- src/AngleSharp.Xml.Tests/Parser/XmlParsing.cs | 8 ++++++++ src/AngleSharp.Xml/Parser/XmlDomBuilder.cs | 6 +++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/AngleSharp.Xml.Tests/Parser/XmlParsing.cs b/src/AngleSharp.Xml.Tests/Parser/XmlParsing.cs index 1bf0f2e..82b4ca9 100644 --- a/src/AngleSharp.Xml.Tests/Parser/XmlParsing.cs +++ b/src/AngleSharp.Xml.Tests/Parser/XmlParsing.cs @@ -146,5 +146,13 @@ public void ParseInvalidXmlShouldNotThrowWhenSuppressingErrors_Issue14() parser.ParseDocument(source); }); } + + [Test] + public async Task XmlPrefixedAttributesShouldLocateXmlNamespaceWithoutDeclaration() + { + var document = @"".ToXmlDocument(); + var root = document.DocumentElement; + Assert.AreEqual(NamespaceNames.XmlUri, root.Attributes.Single().NamespaceUri); + } } } diff --git a/src/AngleSharp.Xml/Parser/XmlDomBuilder.cs b/src/AngleSharp.Xml/Parser/XmlDomBuilder.cs index 728214d..da87479 100644 --- a/src/AngleSharp.Xml/Parser/XmlDomBuilder.cs +++ b/src/AngleSharp.Xml/Parser/XmlDomBuilder.cs @@ -431,7 +431,11 @@ private Attr CreateAttribute(String name, String value) var prefix = name.Substring(0, colon); var ns = NamespaceNames.XmlNsUri; - if (!prefix.Is(NamespaceNames.XmlNsPrefix)) + if (prefix.Is(NamespaceNames.XmlPrefix)) + { + ns = NamespaceNames.XmlUri; + } + else if (!prefix.Is(NamespaceNames.XmlNsPrefix)) { ns = CurrentNode.LookupNamespaceUri(prefix); }