Skip to content

Commit

Permalink
Merge in implementation of unparsed-entity-uri
Browse files Browse the repository at this point in the history
  • Loading branch information
jbowtie committed Nov 20, 2013
2 parents f97652a + 0792939 commit 766b79c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions xslt/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
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 current input node
XPathContext *xpath.XPath //the XPath context
Expand Down
32 changes: 32 additions & 0 deletions xslt/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func (style *Stylesheet) RegisterXsltFunctions() {
style.Functions["key"] = XsltKey
style.Functions["system-property"] = XsltSystemProperty
style.Functions["document"] = XsltDocumentFn
style.Functions["unparsed-entity-uri"] = XsltUnparsedEntityUri
//element-available
//function-available - possibly internal to Gokogiri?
//id - see implementation in match.go
Expand Down Expand Up @@ -89,3 +90,34 @@ func XsltDocumentFn(context xpath.VariableScope, args []interface{}) interface{}
}
return nil
}

// Implementation of unparsed-entity-uri from spec
func XsltUnparsedEntityUri(context xpath.VariableScope, args []interface{}) interface{} {
if len(args) < 1 {
return nil
}
c := context.(*ExecutionContext)
name := argValToString(args[0])
val := c.Source.UnparsedEntityURI(name)
return val
}

// util function because we can't assume we're actually getting a string
func argValToString(val interface{}) (out string) {
if val == nil {
return
}
switch v := val.(type) {
case string:
return v
case []unsafe.Pointer:
if len(v) == 0 {
return
}
n := xml.NewNode(v[0], nil)
out = n.Content()
default:
out = fmt.Sprintf("%v", v)
}
return
}
2 changes: 1 addition & 1 deletion xslt/stylesheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (style *Stylesheet) Process(doc *xml.XmlDocument, options StylesheetOptions
// create output document with appropriate values
output := xml.CreateEmptyDocument(doc.InputEncoding(), doc.OutputEncoding())
// init context node/document
context := &ExecutionContext{Output: output.Me, OutputNode: output, Style: style}
context := &ExecutionContext{Output: output.Me, OutputNode: output, Style: style, Source: doc}
context.Current = doc
context.XPathContext = doc.DocXPathCtx()
// when evaluating keys/global vars position is always 1
Expand Down
2 changes: 1 addition & 1 deletion xslt/stylesheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestXsltGeneral(t *testing.T) {
runGeneralXslTest(t, "bug-48-")
runGeneralXslTest(t, "bug-49-") // global variable defined in terms of inner variable
runGeneralXslTest(t, "bug-50-")
//runGeneralXslTest(t, "bug-52") //unparsed-entity-uri
//runGeneralXslTest(t, "bug-52") //unparsed-entity-uri with nodeset argument
runGeneralXslTest(t, "bug-53") // depends on DTD processing of ATTLIST with default attribute
runGeneralXslTest(t, "bug-54")
runGeneralXslTest(t, "bug-55")
Expand Down

0 comments on commit 766b79c

Please sign in to comment.