Skip to content
A Swift package for working with HTML, XML, and other markup languages, based on libxml2.
Swift Other
  1. Swift 98.9%
  2. Other 1.1%
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/workflows Add support for Swift 5.1 Mar 27, 2020
Modules Add support for Swift 5.1 Mar 27, 2020
Sources Add support for Swift 5.1 Mar 27, 2020
Tests Add XPath methods to element Mar 20, 2020
.gitignore Initial commit Mar 19, 2020
Brewfile Add support for Swift 5.1 Mar 27, 2020
LICENSE.md Add LICENSE.md Mar 20, 2020
Package.swift Add support for Swift 5.1 Mar 27, 2020
README.md Add support for Swift 5.1 Mar 27, 2020

README.md

Markup

CI Documentation

A Swift package for working with HTML, XML, and other markup languages, based on libxml2.

This project is under active development and is not ready for production use.

Features

  • XML Support
  • XHTML4 Support
  • XPath Expression Evaluation
  • HTML5 Support (using Gumbo)
  • CSS Selector to XPath Functionality*
  • XML Namespace Support*
  • DTD and Relax-NG Validation*
  • XInclude Support*
  • XSLT Support*
  • SAX Parser Interface*
  • HTML and XML Function Builder Interfaces*

* Coming soon!

Requirements

  • Swift 5.1+
  • libxml2 (except for macOS with Xcode 11.4 or later)

Usage

XML

Parsing & Introspection

import XML

let xml = #"""
<?xml version="1.0" encoding="UTF-8"?>
<!-- begin greeting -->
<greeting>Hello!</greeting>
<!-- end greeting -->
"""#

let document = try XML.Document(string: xml)!
document.root?.name // "greeting"
document.root?.content // "Hello!"

document.children.count // 3 (two comment nodes and one element node)
document.root?.children.count // 1 (one text node)

Searching and XPath Expression Evaluation

document.search("//greeting").count // 1
document.evaluate("//greeting/text()") // .string("Hello!")

Modification

for case let comment as Comment in document.children {
    comment.remove()
}

document.root?.name = "valediction"
document.root?["lang"] = "it"
document.root?.content = "Arrivederci!"

document.description // =>
/*
<?xml version="1.0" encoding="UTF-8"?>
<valediction lang="it">Arrivederci!</valediction>

*/

HTML

Parsing & Introspection

import HTML

let html = #"""
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome</title>
</head>
<body>
    <p>Hello, world!</p>
</body>
</html>
"""#

let document = try HTML.Document(string: html)!
document.body?.children.count // 1 (one element node)
document.body?.children.first?.name // "p"
document.body?.children.first?.text // "Hello, world!"

Searching and XPath Expression Evaluation

document.search("/body/p").count // 1
document.search("/body/p").first?.xpath // "/body/p[0]"
document.evaluate("/body/p/text()") // .string("Hello, world!")

Creation and Modification

let div = Element(name: "div")
div["class"] = "wrapper"
if let p = document.search("/body/p").first {
    p.wrap(inside: div)
}

document.body?.description // =>
/*
<div class="wrapper">
    <p>Hello, world!</p>
</div>
*/

Installation

Swift Package Manager

If you're on Linux or if you're on macOS and using Xcode < 11.4, install the libxml2 system library:

# macOS for Xcode 11.3 and earlier
$ brew install libxml2
$ brew link --force libxml2

# Linux (Ubuntu)
$ sudo apt-get install libxml2-dev

Add the Markup package to your target dependencies in Package.swift:

import PackageDescription

let package = Package(
  name: "YourProject",
  dependencies: [
    .package(
        url: "https://github.com/SwiftDocOrg/Markup",
        from: "0.0.1"
    ),
  ]
)

Add Markup as a dependency to your target(s):

targets: [
.target(
    name: "YourTarget",
    dependencies: ["Markup"]),

License

MIT

Contact

Mattt (@mattt)

You can’t perform that action at this time.