SWXMLHash icon indicating copy to clipboard operation
SWXMLHash copied to clipboard

Elements with namespaces have their element names "switched"

Open raheelahmad opened this issue 6 years ago • 4 comments

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

raheelahmad avatar Mar 07 '18 05:03 raheelahmad

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!

drmohundro avatar Mar 07 '18 06:03 drmohundro

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.

raheelahmad avatar Mar 07 '18 06:03 raheelahmad

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.

drmohundro avatar Mar 09 '18 03:03 drmohundro

setting shouldProcessNamespaces to true does work. Thanks!

raheelahmad avatar Mar 09 '18 15:03 raheelahmad