Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ struct XMLCoderElement: Equatable, Sendable {
}

func toXMLString(
with header: XMLHeader?,
header: XMLHeader?,
stylesheet: XMLStylesheet?,
doctype: XMLDocumentType?,
escapedCharacters: (elements: [(String, String)], attributes: [(String, String)]),
formatting: XMLEncoder.OutputFormatting,
Expand All @@ -159,6 +160,10 @@ struct XMLCoderElement: Equatable, Sendable {
base += headerXML
}

if let stylesheet = stylesheet, let stylesheetXML = stylesheet.toXML() {
base += stylesheetXML
}

if let doctype = doctype {
base += doctype.toXML()
}
Expand Down
48 changes: 48 additions & 0 deletions Sources/XMLCoder/Auxiliaries/XMLStylesheet.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2018-2026 XMLCoder contributors
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
//
// Created by Mateusz Jabłoński on 06/21/26.
//

import Foundation

/// Type that allows overriding XML stylesheet during encoding. Pass a value of this type to the `encode`
/// function of `XMLEncoder` to specify the exact value of the header you'd like to see in the encoded
/// data.
/// Specification: https://www.w3.org/TR/xml-stylesheet/
public struct XMLStylesheet: Sendable {
/// Gives the address of the referenced style sheet.
public let href: String

/// Gives an advisory media type for the referenced style sheet.
public let type: String?

/// Gives the title of the referenced style sheet in a style sheet set.
public let title : String?

public init(href: String, type: String? = nil, title: String? = nil) {
self.href = href
self.type = type
self.title = title
}

func toXML() -> String? {
var string = "<?xml-stylesheet"

string += " href=\"\(href)\""

if let type = type {
string += " type=\"\(type)\""
}

if let title = title {
string += " title=\"\(title)\""
}

string += "?>\n"

return string
}
}
6 changes: 4 additions & 2 deletions Sources/XMLCoder/Encoder/XMLEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,9 @@ open class XMLEncoder {
/// - throws: An error if any value throws an error during encoding.
open func encode<T: Encodable>(_ value: T,
withRootKey rootKey: String? = nil,
rootAttributes: [String: String]? = nil,
rootAttributes: [(key: String, value: String)]? = nil,
header: XMLHeader? = nil,
stylesheet: XMLStylesheet? = nil,
doctype: XMLDocumentType? = nil) throws -> Data
{
let encoder = XMLEncoderImplementation(options: options, nodeEncodings: [])
Expand Down Expand Up @@ -409,7 +410,8 @@ open class XMLEncoder {
}

return element.toXMLString(
with: header,
header: header,
stylesheet: stylesheet,
doctype: doctype,
escapedCharacters: (
elements: charactersEscapedInElements,
Expand Down
6 changes: 4 additions & 2 deletions Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ class XMLElementTests: XCTestCase {
attributes: [inputNamespace])

let result = input.toXMLString(
with: nil,
header: nil,
stylesheet: nil,
doctype: nil,
escapedCharacters: (elements: XMLEncoder().charactersEscapedInElements,
attributes: XMLEncoder().charactersEscapedInAttributes),
Expand Down Expand Up @@ -122,7 +123,8 @@ class XMLElementTests: XCTestCase {
attributes: [inputNamespace])

let result = input.toXMLString(
with: nil,
header: nil,
stylesheet: nil,
doctype: nil,
escapedCharacters: (elements: XMLEncoder().charactersEscapedInElements,
attributes: XMLEncoder().charactersEscapedInAttributes),
Expand Down
6 changes: 3 additions & 3 deletions Tests/XMLCoderTests/RootLevetExtraAttributesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ final class RootLevetExtraAttributesTests: XCTestCase {
let policy = Policy(name: "test", initial: "extra root attributes")

let extraRootAttributes = [
"xmlns": "http://www.nrf-arts.org/IXRetail/namespace",
"xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
("xmlns", "http://www.nrf-arts.org/IXRetail/namespace"),
("xmlns:xsd", "http://www.w3.org/2001/XMLSchema"),
("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"),
]

encoder.keyEncodingStrategy = .lowercased
Expand Down