Skip to content

Instantly share code, notes, and snippets.

@hironytic
Created March 24, 2019 13:15
Show Gist options
  • Select an option

  • Save hironytic/d4812f2f104fccbf0f7f8aeb19a89a35 to your computer and use it in GitHub Desktop.

Select an option

Save hironytic/d4812f2f104fccbf0f7f8aeb19a89a35 to your computer and use it in GitHub Desktop.
Attributes with namespace (Foundation on macOS)
import Cocoa
let uriNs1 = "http://example.com/ns1"
let uriNs2 = "http://example.com/ns2"
let root = XMLNode.element(withName: "root") as! XMLElement
root.addNamespace(XMLNode.namespace(withName: "ns1", stringValue: uriNs1) as! XMLNode)
let element = XMLNode.element(withName: "element") as! XMLElement
element.addNamespace(XMLNode.namespace(withName: "ns2", stringValue: uriNs2) as! XMLNode)
root.addChild(element)
// Add attributes without URI
element.addAttribute(XMLNode.attribute(withName: "name", stringValue: "John") as! XMLNode)
element.addAttribute(XMLNode.attribute(withName: "ns1:name", stringValue: "Tom") as! XMLNode)
// Add attributes with URI
element.addAttribute(XMLNode.attribute(withName: "ns1:age", uri: uriNs1, stringValue: "44") as! XMLNode)
element.addAttribute(XMLNode.attribute(withName: "ns1:address", uri: uriNs2, stringValue: "Foobar City") as! XMLNode) // different namespace prefix
element.addAttribute(XMLNode.attribute(withName: "phone", uri: uriNs2, stringValue: "xxx-xxx") as! XMLNode) // no prefix
// Retrieve attributes without URI
element.attribute(forName: "name") // name="John"
element.attribute(forName: "ns1:name") // ns1:name="Tom"
element.attribute(forName: "ns1:age") // ns1:age="44"
element.attribute(forName: "ns1:address") // ns1:address="Foobar City"
element.attribute(forName: "ns2:address") // ns1:address="Foobar City"
element.attribute(forName: "phone") // phone="xxx-xxx"
element.attribute(forName: "ns2:phone") // phone="xxx-xxx"
// Retrieve attributes with URI
element.attribute(forLocalName: "name", uri: nil) // name="John"
element.attribute(forLocalName: "name", uri: uriNs1) // ns1:name="Tom"
element.attribute(forLocalName: "age", uri: uriNs1) // ns1:age="44"
element.attribute(forLocalName: "address", uri: uriNs1) // nil
element.attribute(forLocalName: "address", uri: uriNs2) // ns1:address="Foobar City"
element.attribute(forLocalName: "phone", uri: uriNs2) // phone="xxx-xxx"
element.attribute(forLocalName: "ns1:age", uri: uriNs1) // nil
// Overwrite attributes
element.addAttribute(XMLNode.attribute(withName: "ns1:age", stringValue: "33") as! XMLNode)
element.attribute(forName: "ns1:age") // ns1:age="33"
element.addAttribute(XMLNode.attribute(withName: "ns1:name", uri: uriNs1, stringValue: "Tommy") as! XMLNode)
element.attribute(forLocalName: "name", uri: uriNs1) // ns1:name="Tommy"
// Remove attributes
element.removeAttribute(forName: "name")
element.attribute(forLocalName: "name", uri: nil) // nil
element.attribute(forLocalName: "name", uri: uriNs1) // ns1:name="Tommy"
element.removeAttribute(forName: "ns1:name")
element.attribute(forLocalName: "name", uri: uriNs1) // nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment