Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-11606] XMLElement.attribute(forName:) broken on Linux #3284

Open
florianreinhart opened this issue Oct 13, 2019 · 2 comments
Open

[SR-11606] XMLElement.attribute(forName:) broken on Linux #3284

florianreinhart opened this issue Oct 13, 2019 · 2 comments

Comments

@florianreinhart
Copy link

Previous ID SR-11606
Radar None
Original Reporter @florianreinhart
Type Bug
Environment

Swift version 5.1.1 (swift-5.1.1-RELEASE)

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug, Linux
Assignee None
Priority Medium

md5: 454e540889fcb0bbebb5cd7f49ac3454

relates to:

  • SR-10424 Parity: XMLElement: XML Namespaces

Issue Description:

I ran into a regression probably introduced in FoundationXML bundled with Swift 5.1 by PR2313.

XMLElement.attribute(forName🙂 does not return an attribute if xmlns is set to an empty string in the element.

The following program demonstrates the issue:

import Foundation
#if canImport(FoundationXML)
import FoundationXML
#endif


func parseXML(_ xmlData: Data) {

    let xmlDocument = try! XMLDocument(data: xmlData)

    let rootElement = xmlDocument.rootElement()!

    let child = rootElement.children?.first(where: { $0.localName == "child" }) as! XMLElement
    print("child attributes: \(child.attributes?.description ?? "nil")")
    print("child attribute1: \(child.attribute(forName: "attribute1")?.description ?? "nil")")
}


// XML without xmlns
let xmlData1 = Data("""
<rootElement>
    <child attribute1="value1">Child Content</child>
</rootElement>
""".utf8)

parseXML(xmlData1)


// XML with xmlns set to empty string
let xmlData2 = Data("""
<rootElement>
    <child xmlns="" attribute1="value1">Child Content</child>
</rootElement>
""".utf8)

parseXML(xmlData2)

On macOS the output is:

child attributes: [attribute1="value1"]
child attribute1: attribute1="value1"
child attributes: [attribute1="value1"]
child attribute1: attribute1="value1"

And on Linux:

child attributes: [ attribute1="value1"]
child attribute1:  attribute1="value1"
child attributes: [ attribute1="value1"]
child attribute1: nil

Also notice the additional space in front of the attribute name on Linux.

@florianreinhart
Copy link
Author

CC @millenomi

@weissi
Copy link
Member

weissi commented Apr 20, 2021

Still not fixed in current main

root@0ae6b2f0fd1a:/Users/johannes/devel/swift-corelibs-foundation# swift
Welcome to Swift version 5.5-dev (LLVM fb5a91b4e892ed9, Swift f17142883421ec0).
Type :help for assistance.
  1> import Foundation 
  2. #if canImport(FoundationXML) 
  3. import FoundationXML 
  4. #endif 
  5.  
  6.  
  7. func parseXML(_ xmlData: Data) { 
  8.  
  9.     let xmlDocument = try! XMLDocument(data: xmlData) 
 10.  
 11.     let rootElement = xmlDocument.rootElement()! 
 12.  
 13.     let child = rootElement.children?.first(where: { $0.localName == "child" }) as! XMLElement 
 14.     print("child attributes: \(child.attributes?.description ?? "nil")") 
 15.     print("child attribute1: \(child.attribute(forName: "attribute1")?.description ?? "nil")") 
 16. } 
 17.  
 18.  
 19. // XML without xmlns 
 20. let xmlData1 = Data(""" 
 21. <rootElement> 
 22.     <child attribute1="value1">Child Content</child> 
 23. </rootElement> 
 24. """.utf8) 
 25.  
 26. parseXML(xmlData1) 
 27.  
 28.  
 29. // XML with xmlns set to empty string 
 30. let xmlData2 = Data(""" 
 31. <rootElement> 
 32.     <child xmlns="" attribute1="value1">Child Content</child> 
 33. </rootElement> 
 34. """.utf8) 
 35.  
 36. parseXML(xmlData2)
child attributes: [ attribute1="value1"]
child attribute1:  attribute1="value1"
child attributes: [ attribute1="value1"]
child attribute1: nil
xmlData1: Data = 81 bytes
xmlData2: Data = 90 bytes

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants