Open stevenbrix opened 2 months ago
If the root of the XML document has a default xmlns namespace definition, then XMLElement.attribute(forName:) will incorrectly return nil.
XMLElement.attribute(forName:)
nil
<?xml version="1.0" encoding="utf-8"?> <AppInstaller Version="99.99.99.100" xmlns="http://schemas.microsoft.com/appx/appinstaller/2018"> <MainPackage Version="99.99.99.100"/> </AppInstaller>
let xmlFile = "file:///c:/temp/foo.xml" let doc = try XMLDocument(contentsOf: URL(fileURLWithPath: xmlFile)!) let mainPackage = doc.rootElement()!.elements(forName: "MainPackage")[0] print(mainPackage.attribute(forName: "Version"))
swift run
Console prints Optional( Version="99.99.99.100")
Optional( Version="99.99.99.100")
Console prints nil
xmlns="http://schemas.microsoft.com/appx/appinstaller/2018"
private extension XMLElement { func knownAttributeValue(forName: String) -> String { for attribute in attributes ?? [] { guard forName == attribute.name, let value = attribute.stringValue else { continue } return value } fatalError("No attribute named \(forName) found") } }
If the root of the XML document has a default xmlns namespace definition, then
XMLElement.attribute(forName:)
will incorrectly returnnil
.Repro steps
swift run
the projectExpected
Console prints
Optional( Version="99.99.99.100")
Actual
Console prints
nil
Other Notes
xmlns="http://schemas.microsoft.com/appx/appinstaller/2018"
from the root doc makes things work as expected.