Last active
August 16, 2019 15:33
-
-
Save stonezhl/c669d5bf46431b02155e18abc82babda to your computer and use it in GitHub Desktop.
ContainerDocument Struct
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Kanna | |
struct ContainerDocument { | |
let opfPath: String | |
let document: XMLDocument | |
init?(url: URL) { | |
do { | |
// Just one line to get the DOM document of a XML file | |
document = try Kanna.XML(url: url, encoding: .utf8) | |
// Create namespace for the 'container' element | |
let namespace = ["ctn": "urn:oasis:names:tc:opendocument:xmlns:container"] | |
// Our first XPath query | |
let xpath = "//ctn:rootfile[@full-path]/@full-path" | |
// Execute the XPath query by 'at_xpath' | |
guard let path = document.at_xpath(xpath, namespaces: XPath.container.namespace)?.text else { return nil } | |
opfPath = path | |
} catch { | |
print("Parsing the XML file at \(url) failed with error: \(error)") | |
return nil | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment