xslt

package
v0.0.0-...-3140c0a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2020 License: MIT Imports: 16 Imported by: 5

Documentation

Overview

Package xslt provides an XSLT 1.0 processor.

Index

Examples

Constants

View Source
const XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace"
View Source
const XSLT_NAMESPACE = "http://www.w3.org/1999/XSL/Transform"

Variables

This section is empty.

Functions

func EXSLTmathabs

func EXSLTmathabs(context xpath.VariableScope, args []interface{}) interface{}

func EXSLTmathconstant

func EXSLTmathconstant(context xpath.VariableScope, args []interface{}) interface{}

func EXSLTmathcos

func EXSLTmathcos(context xpath.VariableScope, args []interface{}) interface{}

func EXSLTmathsin

func EXSLTmathsin(context xpath.VariableScope, args []interface{}) interface{}

func EXSLTnodeset

func EXSLTnodeset(context xpath.VariableScope, args []interface{}) interface{}

func IsBlank

func IsBlank(xmlnode xml.Node) bool

Returns true if the node is a whitespace-only text node

func IsXsltName

func IsXsltName(xmlnode xml.Node, name string) bool

Returns true if the node is in the XSLT namespace

func ResolveAlias

func ResolveAlias(style *Stylesheet, alias, auri string) (prefix, uri string)

func XsltCurrent

func XsltCurrent(context xpath.VariableScope, args []interface{}) interface{}

Implementation of current() from XSLT spec

func XsltDocumentFn

func XsltDocumentFn(context xpath.VariableScope, args []interface{}) interface{}

Implementation of document() from XSLT spec

func XsltElementAvailable

func XsltElementAvailable(context xpath.VariableScope, args []interface{}) interface{}

Implementation of element-available() from XSLT spec

func XsltFunctionAvailable

func XsltFunctionAvailable(context xpath.VariableScope, args []interface{}) interface{}

Implementation of function-available() from XSLT spec

func XsltGenerateId

func XsltGenerateId(context xpath.VariableScope, args []interface{}) interface{}

Implementation of generate-id() from XSLT spec

func XsltKey

func XsltKey(context xpath.VariableScope, args []interface{}) interface{}

Implementation of key() from XSLT spec

func XsltSystemProperty

func XsltSystemProperty(context xpath.VariableScope, args []interface{}) interface{}

Implementation of system-property() from XSLT spec

func XsltUnparsedEntityUri

func XsltUnparsedEntityUri(context xpath.VariableScope, args []interface{}) interface{}

Implementation of unparsed-entity-uri() from XSLT spec

Types

type CompiledMatch

type CompiledMatch struct {
	Steps    []*MatchStep
	Template *Template
	// contains filtered or unexported fields
}

The compiled match pattern

func CompileMatch

func CompileMatch(s string, t *Template) (matches []*CompiledMatch)

func (*CompiledMatch) DefaultPriority

func (m *CompiledMatch) DefaultPriority() (priority float64)

func (*CompiledMatch) EvalMatch

func (m *CompiledMatch) EvalMatch(node xml.Node, mode string, context *ExecutionContext) bool

Returns true if the node matches the pattern

func (*CompiledMatch) Hash

func (m *CompiledMatch) Hash() (hash string)

func (*CompiledMatch) IsAttr

func (m *CompiledMatch) IsAttr() bool

func (*CompiledMatch) IsComment

func (m *CompiledMatch) IsComment() bool

func (*CompiledMatch) IsElement

func (m *CompiledMatch) IsElement() bool

func (*CompiledMatch) IsIdKey

func (m *CompiledMatch) IsIdKey() bool

func (*CompiledMatch) IsNode

func (m *CompiledMatch) IsNode() bool

func (*CompiledMatch) IsPI

func (m *CompiledMatch) IsPI() bool

func (*CompiledMatch) IsText

func (m *CompiledMatch) IsText() bool

type CompiledStep

type CompiledStep interface {
	Compile(node xml.Node)
	Apply(node xml.Node, context *ExecutionContext)
}

func CompileSingleNode

func CompileSingleNode(node xml.Node) (step CompiledStep)

type ExecutionContext

type ExecutionContext struct {
	Style          *Stylesheet                 // The master stylesheet
	Output         xml.Document                // The output document
	Source         xml.Document                // The source input document
	OutputNode     xml.Node                    // The current output node
	Current        xml.Node                    // The node that will be returned for "current()"
	XPathContext   *xpath.XPath                //the XPath context
	Mode           string                      //The current template mode
	Stack          list.List                   //stack used for scoping local variables
	InputDocuments map[string]*xml.XmlDocument //additional input documents via document()
}

ExecutionContext is passed to XSLT instructions during processing.

func (*ExecutionContext) ChildrenOf

func (context *ExecutionContext) ChildrenOf(node xml.Node) (children []xml.Node)

ChildrenOf returns the node children, ignoring any whitespace-only text nodes that are stripped by strip-space or xml:space

func (*ExecutionContext) DeclareLocalVariable

func (context *ExecutionContext) DeclareLocalVariable(name, ns string, v *Variable) error

func (*ExecutionContext) DeclareStylesheetNamespacesIfRoot

func (context *ExecutionContext) DeclareStylesheetNamespacesIfRoot(node xml.Node)

Propagate namespaces to the root of the output document

func (*ExecutionContext) DefaultNamespace

func (context *ExecutionContext) DefaultNamespace(node xml.Node) string

Determine the default namespace currently defined in scope

func (*ExecutionContext) EvalXPath

func (context *ExecutionContext) EvalXPath(xmlNode xml.Node, data interface{}) (result interface{}, err error)

func (*ExecutionContext) EvalXPathAsBoolean

func (context *ExecutionContext) EvalXPathAsBoolean(xmlNode xml.Node, data interface{}) (result bool)

func (*ExecutionContext) EvalXPathAsNodeset

func (context *ExecutionContext) EvalXPathAsNodeset(xmlNode xml.Node, data interface{}) (result xml.Nodeset, err error)

func (*ExecutionContext) EvalXPathAsString

func (context *ExecutionContext) EvalXPathAsString(xmlNode xml.Node, data interface{}) (result string, err error)

func (*ExecutionContext) FetchInputDocument

func (context *ExecutionContext) FetchInputDocument(loc string, relativeToSource bool) (doc *xml.XmlDocument)

func (*ExecutionContext) FindVariable

func (context *ExecutionContext) FindVariable(name, ns string) (ret *Variable)

func (*ExecutionContext) IsFunctionRegistered

func (context *ExecutionContext) IsFunctionRegistered(name, ns string) bool

func (*ExecutionContext) LookupLocalVariable

func (context *ExecutionContext) LookupLocalVariable(name, ns string) (ret *Variable)

func (*ExecutionContext) LookupNamespace

func (context *ExecutionContext) LookupNamespace(prefix string, node xml.Node) (uri string)

Attempt to map a prefix to a URI.

func (*ExecutionContext) PopStack

func (context *ExecutionContext) PopStack()

leave the variable scope

func (*ExecutionContext) PushStack

func (context *ExecutionContext) PushStack()

create a local scope for variable resolution

func (*ExecutionContext) RegisterXPathNamespaces

func (context *ExecutionContext) RegisterXPathNamespaces(node xml.Node) (err error)

Register the namespaces in scope with libxml2 so that XPaths with namespaces are resolved correctly.

libxml2 probably already makes this info available

func (*ExecutionContext) ResolveFunction

func (context *ExecutionContext) ResolveFunction(name, ns string) xpath.XPathFunction

func (*ExecutionContext) ResolveQName

func (context *ExecutionContext) ResolveQName(qname string) (ns, name string)

func (*ExecutionContext) ResolveVariable

func (context *ExecutionContext) ResolveVariable(name, ns string) (ret interface{})

func (*ExecutionContext) ShouldStrip

func (context *ExecutionContext) ShouldStrip(xmlNode xml.Node) bool

ShouldStrip evaluates the strip-space, preserve-space, and xml:space rules and returns true if a node is a whitespace-only text node that should be stripped.

func (*ExecutionContext) UseCDataSection

func (context *ExecutionContext) UseCDataSection(node xml.Node) bool

type Key

type Key struct {
	// contains filtered or unexported fields
}

type LiteralResultElement

type LiteralResultElement struct {
	Node     xml.Node
	Children []CompiledStep
}

Literal result elements are any elements in a template that are not in the xsl namespace. They are copied to the output document.

func (*LiteralResultElement) Apply

func (e *LiteralResultElement) Apply(node xml.Node, context *ExecutionContext)

func (*LiteralResultElement) Compile

func (e *LiteralResultElement) Compile(node xml.Node)

func (*LiteralResultElement) IsExtension

func (e *LiteralResultElement) IsExtension(node xml.Node, context *ExecutionContext) bool

type MatchStep

type MatchStep struct {
	Op    StepOperation
	Value string
}

An individual step in the pattern

type NodeSorter

type NodeSorter struct {
	// contains filtered or unexported fields
}

func (*NodeSorter) Len

func (s *NodeSorter) Len() int

func (*NodeSorter) Less

func (s *NodeSorter) Less(i, j int) bool

func (*NodeSorter) Swap

func (s *NodeSorter) Swap(i, j int)

type RomanNumber

type RomanNumber int

func (RomanNumber) String

func (n RomanNumber) String() (out string)

type StepOperation

type StepOperation int
const (
	OP_END StepOperation = iota
	OP_ROOT
	OP_ELEM
	OP_ATTR
	OP_PARENT
	OP_ANCESTOR
	OP_ID
	OP_KEY
	OP_NS
	OP_ALL
	OP_PI
	OP_COMMENT
	OP_TEXT
	OP_NODE
	OP_PREDICATE
	OP_OR
	OP_ERROR
)

type Stylesheet

type Stylesheet struct {
	Doc               *xml.XmlDocument
	Parent            *Stylesheet //xsl:import
	NamedTemplates    map[string]*Template
	NamespaceMapping  map[string]string
	NamespaceAlias    map[string]string
	ElementMatches    map[string]*list.List //matches on element name
	AttrMatches       map[string]*list.List //matches on attr name
	NodeMatches       *list.List            //matches on node()
	TextMatches       *list.List            //matches on text()
	PIMatches         *list.List            //matches on processing-instruction()
	CommentMatches    *list.List            //matches on comment()
	IdKeyMatches      *list.List            //matches on id() or key()
	Imports           *list.List
	Variables         map[string]*Variable
	Functions         map[string]xpath.XPathFunction
	AttributeSets     map[string]CompiledStep
	ExcludePrefixes   []string
	ExtensionPrefixes []string
	StripSpace        []string
	PreserveSpace     []string
	CDataElements     []string
	GlobalParameters  []string

	Keys               map[string]*Key
	OutputMethod       string //html, xml, text
	DesiredEncoding    string //encoding specified by xsl:output
	OmitXmlDeclaration bool   //defaults to false
	IndentOutput       bool   //defaults to false
	Standalone         bool   //defaults to false
	// contains filtered or unexported fields
}

Stylesheet is an XSLT 1.0 processor.

func ParseStylesheet

func ParseStylesheet(doc *xml.XmlDocument, fileuri string) (style *Stylesheet, err error)

ParseStylesheet compiles the stylesheet's XML representation and returns a Stylesheet instance.

The fileuri argument is used to resolve relative paths for xsl:import and xsl:include instructions and should generally be the filename of the stylesheet. If you pass an empty string, the working directory will be used for path resolution.

Example

sample usage for parse stylesheet

//parse the stylesheet
style, _ := xml.ReadFile("testdata/test.xsl", xml.StrictParseOption)
stylesheet, _ := ParseStylesheet(style, "testdata/test.xsl")

//process the input
input, _ := xml.ReadFile("testdata/test.xml", xml.StrictParseOption)
output, _ := stylesheet.Process(input, StylesheetOptions{false, nil})
fmt.Println(output)
Output:

func (*Stylesheet) IsExcluded

func (style *Stylesheet) IsExcluded(prefix string) bool

func (*Stylesheet) LookupAttributeSet

func (style *Stylesheet) LookupAttributeSet(name string) CompiledStep

Locate an attribute set by name

func (*Stylesheet) LookupTemplate

func (style *Stylesheet) LookupTemplate(node xml.Node, mode string, context *ExecutionContext) (template *Template)

If there is no matching template, nil is returned.

func (*Stylesheet) ParseTemplate

func (style *Stylesheet) ParseTemplate(node xml.Node)

ParseTemplate parses and compiles the xsl:template elements.

func (*Stylesheet) Process

func (style *Stylesheet) Process(doc *xml.XmlDocument, options StylesheetOptions) (out string, err error)

The output is not guaranteed to be well-formed XML, so the serialized string is returned. Consideration is being given to returning a slice of bytes and encoding information.

func (*Stylesheet) RegisterAttributeSet

func (style *Stylesheet) RegisterAttributeSet(node xml.Node)

func (*Stylesheet) RegisterGlobalVariable

func (style *Stylesheet) RegisterGlobalVariable(node xml.Node)

func (*Stylesheet) RegisterXsltFunctions

func (style *Stylesheet) RegisterXsltFunctions()

type StylesheetOptions

type StylesheetOptions struct {
	IndentOutput bool                   //force the output to be indented
	Parameters   map[string]interface{} //supply values for stylesheet parameters
}

StylesheetOptions to control processing. Parameters values are passed into the stylesheet via this structure.

type Template

type Template struct {
	Name     string
	Mode     string
	Match    string
	Priority float64
	Children []CompiledStep
	Node     xml.Node
}

func (*Template) AddChild

func (template *Template) AddChild(child CompiledStep)

func (*Template) Apply

func (template *Template) Apply(node xml.Node, context *ExecutionContext, params []*Variable)

func (*Template) CompileContent

func (template *Template) CompileContent(node xml.Node)

type TextOutput

type TextOutput struct {
	Content string
}

Stylesheet text nodes

func (*TextOutput) Apply

func (t *TextOutput) Apply(node xml.Node, context *ExecutionContext)

func (*TextOutput) Compile

func (t *TextOutput) Compile(node xml.Node)

type Variable

type Variable struct {
	Name     string
	Node     xml.Node
	Children []CompiledStep
	Value    interface{}
}

Used to represent an xsl:variable or xsl:param

func (*Variable) Apply

func (i *Variable) Apply(node xml.Node, context *ExecutionContext)

Applying a variable node is calculating its value.

func (*Variable) Compile

func (i *Variable) Compile(node xml.Node)

Compile the variable.

TODO: compile the XPath expression and determine if it is a constant

type XsltInstruction

type XsltInstruction struct {
	Node     xml.Node
	Name     string
	Children []CompiledStep
	// contains filtered or unexported fields
}

Most xsl elements are compiled to an instruction.

func (*XsltInstruction) Apply

func (i *XsltInstruction) Apply(node xml.Node, context *ExecutionContext)

Evaluate an instruction and generate output nodes

func (*XsltInstruction) Compile

func (i *XsltInstruction) Compile(node xml.Node)

Compile the instruction.

TODO: we should validate the structure during this step

func (*XsltInstruction) Sort

func (i *XsltInstruction) Sort(nodes []xml.Node, context *ExecutionContext)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL