SWXMLHash
SWXMLHash copied to clipboard
Elements with namespaces have their element names "switched"
Running the following on Ubuntu (via Docker):
let podcastURL = URL(string: "http://feed.thisamericanlife.org/talpodcast")!
let contents = try String(contentsOf: podcastURL, encoding: .utf8)
let xml = SWXMLHash.parse(contents)
print(contents)
print("-----")
print(xml.description)
will switch an element name (around the :
) when it has a namespace. For e.g., the string contents
has
<itunes:image href="http://www.thisamericanlife.org/sites/all/themes/thislife/images/logo-square-1400.jpg" />
while the xml
print out gives:
<image:itunes href="http://www.thisamericanlife.org/sites/all/themes/thislife/images/logo-square-1400.jpg"></image:itunes>
Note: this is only on Linux; the same code on macOS gives the correct element name.
Platform: ubuntu:16.04 Xcode 9.2, Swift 4.0.3
That is weird. I haven't double checked this yet, but I believe the code that handles this is just (via https://github.com/drmohundro/SWXMLHash/blob/master/Source/SWXMLHash.swift#L960):
xmlReturn.append("<\(name)\(attributesString)>")
I'll have to check to see if the name
property is incorrect, too, because otherwise I can't see how it would have this behavior.
In any event, thanks for the report!
Weird indeed. I would like to blame Foundation.XMLParser
somehow :)
This may be pertinent: https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/XMLParser.swift#L683 but you are probably a better judge.
Okay, I can confirm as well. So, not sure if this helps with the weirdness or not, but if you set shouldProcessNamespaces
to true in the config, it returns the actual element name correctly (without the XML namespace). You can see this at https://gist.github.com/drmohundro/04bc4376d8087711c1058fa4d44ee9c3. That option just gets forwarded on to the underlying Foundation.XMLParser
.
I'll have to dig in on this some more, but I agree that I'm tempted to blame XMLParser as well. It isn't the first difference I've seen with the pure Swift implementation (see #175). Also, the test cases for XMLParser don't appear to have any cases that cover this specific situation - see https://github.com/apple/swift-corelibs-foundation/blob/master/TestFoundation/TestXMLParser.swift.
At this point, I think I'll have to see if I can get a failing test in the Foundation code along with a PR.
setting shouldProcessNamespaces
to true
does work. Thanks!