Swiftpack.co -  SwiftDocOrg/Markup as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
A Swift package for working with HTML, XML, and other markup languages, based on libxml2.
.package(url: "https://github.com/SwiftDocOrg/Markup.git", from: "0.1.2")


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.


  • ☑ 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!


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



Parsing & Introspection

import XML

let xml = #"""
<?xml version="1.0" encoding="UTF-8"?>
<!-- begin 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!")


for case let comment as Comment in document.children {

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>



Parsing & Introspection

import HTML

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

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>

Builder Interface

Available in Swift 5.3+.

import HTML

let document = HTML.Document {
    html(["lang": "en"]) {
        head {
            meta(["charset": "UTF-8"])
            title { "Hello, world!" }

        body(["class": "beautiful"]) {
            div(["class": "wrapper"]) {
                span { "Hello," }
                tag("span") { "world!" }

document.description // =>
<html lang="en">
      <meta charset="UTF-8">
      <title>Hello, world!</title>
  <body class="beautiful">
      <?greeter start>
      <div class="wrapper">
      <?greeter end>



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: [
        url: "https://github.com/SwiftDocOrg/Markup",
        from: "0.1.2"

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

targets: [
    name: "YourTarget",
    dependencies: ["Markup"]),

If you're using Markup in an app, link libxml2 to your target. Open your Xcode project (.xcodeproj) or workspace (.xcworkspace) file, select your top-level project entry in the Project Navigator, and select the target using Markup listed under the Targets heading. Navigate to the "Build Phases" tab, expand "Link Binary With Libraries", and click the + button to add a library. Enter "libxml2" to the search bar, select "libxml2.tbd" from the filtered list, and click the Add button.

Add libxml2 library to your target




Mattt (@mattt)


Stars: 110
Last commit: Yesterday

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API