Documentation
¶
Overview ¶
Package xslt is a pure-Go (CGO_ENABLED=0) XSLT 1.0 transformation engine, the deferred XSLT layer of the go-ruby Nokogiri stack. Ruby's Nokogiri::XSLT is a C wrapper over libxslt; this package instead compiles and applies XSLT 1.0 stylesheets over the pure-Go XML DOM and XPath 1.0 engine provided by github.com/go-ruby-nokogiri/nokogiri, so the whole path stays CGO-free.
Model ¶
A stylesheet is compiled once with Parse (or ParseString) and then applied to any number of source documents with (*Stylesheet).Transform, mirroring
Nokogiri::XSLT(xslt_string).transform(doc) # -> result document Nokogiri::XSLT(xslt_string).apply_to(doc) # -> serialized string
Transform returns the result tree as a *nokogiri.Document; Apply returns the serialized output string honouring xsl:output (method/indent/encoding/ omit-xml-declaration). Stylesheet parameters are passed as a map[string]any whose values are string, float64, bool or *nokogiri.NodeSet.
Coverage ¶
The engine implements XSLT 1.0: xsl:stylesheet/transform, xsl:include and xsl:import (multi-document stylesheets, fetched through a Resolver) with full import-precedence conflict resolution, template rules (match/name/priority/ mode), apply-templates/call-template/apply-imports with conflict resolution by import precedence then priority then document order, the built-in default template rules, and the instruction set: value-of, for-each, if, choose/when/otherwise, variable/param/with-param, copy/copy-of, element/attribute/text/comment/processing-instruction, attribute-set, sort, number, key/key(), literal result elements with attribute-value templates, namespace handling on result elements, xsl:output, and the XSLT function library (document, key, format-number with decimal-format, current, generate-id, system-property, element-available, function-available, unparsed-entity-uri). xsl:decimal-format is honoured by format-number.
Resolving xsl:include / xsl:import ¶
Fetching the stylesheet referenced by an xsl:include or xsl:import is done through the Resolver seam, so compilation needs no filesystem or network of its own. Use ParseStringWithResolver / ParseWithResolver and pass a MapResolver (in-memory), a ResolverFunc, or a custom Resolver. Plain ParseString / Parse have no resolver and reject stylesheets that reference include/import.
Out of scope ¶
XSLT 2.0 / XPath 2.0 features are outside the XSLT 1.0 specification and are not emulated: sequences and the XPath 2.0 data model, xsl:function, xsl:for-each-group (grouping), schema-aware processing (xsl:import-schema, type annotations) and tunnel parameters. This is a 1.0 processor.
Index ¶
Constants ¶
const Version = "1.0"
Version is the XSLT version this processor implements.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MapResolver ¶
MapResolver resolves hrefs from an in-memory map keyed by the exact href string. It is the convenient resolver for tests and for self-contained stylesheet bundles: no filesystem is touched. The base URI is not consulted; each returned module's base URI is its own href, so nested includes/imports are looked up by their own href keys.
type Resolver ¶
type Resolver interface {
Resolve(href, base string) (source string, resolvedBase string, err error)
}
Resolver fetches the source text of a stylesheet referenced by xsl:include or xsl:import. Resolve receives the raw value of the href attribute and the base URI of the referencing stylesheet module (empty for the top-level stylesheet). It returns the referenced stylesheet source and the base URI to associate with it (used to resolve hrefs nested inside the returned stylesheet).
The seam keeps compilation free of any filesystem or network dependency: a stylesheet bundle can be resolved entirely in memory (see MapResolver) or from disk with a small ResolverFunc. When a stylesheet references xsl:include or xsl:import and no Resolver is configured, compilation fails with a clear error rather than silently dropping the reference.
type ResolverFunc ¶
ResolverFunc adapts an ordinary function to the Resolver interface.
type Stylesheet ¶
type Stylesheet struct {
// contains filtered or unexported fields
}
Stylesheet is a compiled XSLT 1.0 stylesheet, ready to Transform any number of source documents.
func Parse ¶
func Parse(doc *nokogiri.Document) (*Stylesheet, error)
Parse compiles an already-parsed stylesheet document. A stylesheet that uses xsl:include or xsl:import needs a Resolver; use ParseWithResolver for those.
func ParseString ¶
func ParseString(src string) (*Stylesheet, error)
ParseString compiles an XSLT stylesheet from its source text. A stylesheet that uses xsl:include or xsl:import needs a Resolver; use ParseStringWithResolver for those.
func ParseStringWithResolver ¶
func ParseStringWithResolver(src string, r Resolver) (*Stylesheet, error)
ParseStringWithResolver compiles an XSLT stylesheet from its source text, fetching any xsl:include / xsl:import references through r.
func ParseWithResolver ¶
func ParseWithResolver(doc *nokogiri.Document, r Resolver) (*Stylesheet, error)
ParseWithResolver compiles an already-parsed stylesheet document, fetching any xsl:include / xsl:import references through r.
