diff --git a/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift b/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift index ab7f901f..45803a6b 100644 --- a/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift +++ b/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift @@ -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, @@ -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() } diff --git a/Sources/XMLCoder/Auxiliaries/XMLStylesheet.swift b/Sources/XMLCoder/Auxiliaries/XMLStylesheet.swift new file mode 100644 index 00000000..f3bc8af2 --- /dev/null +++ b/Sources/XMLCoder/Auxiliaries/XMLStylesheet.swift @@ -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 = "\n" + + return string + } +} diff --git a/Sources/XMLCoder/Encoder/XMLEncoder.swift b/Sources/XMLCoder/Encoder/XMLEncoder.swift index 3444c733..a0f92630 100644 --- a/Sources/XMLCoder/Encoder/XMLEncoder.swift +++ b/Sources/XMLCoder/Encoder/XMLEncoder.swift @@ -360,8 +360,9 @@ open class XMLEncoder { /// - throws: An error if any value throws an error during encoding. open func encode(_ 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: []) @@ -409,7 +410,8 @@ open class XMLEncoder { } return element.toXMLString( - with: header, + header: header, + stylesheet: stylesheet, doctype: doctype, escapedCharacters: ( elements: charactersEscapedInElements, diff --git a/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift b/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift index e7639de0..249e68ad 100644 --- a/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift +++ b/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift @@ -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), @@ -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), diff --git a/Tests/XMLCoderTests/RootLevetExtraAttributesTests.swift b/Tests/XMLCoderTests/RootLevetExtraAttributesTests.swift index 01eb66da..246acd92 100644 --- a/Tests/XMLCoderTests/RootLevetExtraAttributesTests.swift +++ b/Tests/XMLCoderTests/RootLevetExtraAttributesTests.swift @@ -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