# Changes for page XSLT Macro

Last modified by Helge Dahl on 2018/12/07 11:35

#### Export

#### Office Formats

Export as PDF Export as ODT Export as RTF

#### Other Formats

Export as HTML
From empty
To version 1.1
edited by Helge Dahl
on 2018/12/07 11:32
Change comment: Install extension [vincentmassol:xslt-macro/1.1]

## Summary

- Page properties (5 modified, 0 added, 0 removed)
- Attachments (0 modified, 2 added, 0 removed)
  - persons.xml
  - persons.xsl
- Objects (0 modified, 4 added, 0 removed)
  - XWiki.WikiMacroClass[0]
  - XWiki.WikiMacroParameterClass[0]
  - XWiki.WikiMacroParameterClass[1]
  - XWiki.WikiMacroParameterClass[2]

## Details

Page properties
  Title

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +XSLT Macro

  Parent

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +Main.WebHome

  Author

    ... | ... | @@ -1,1 +1,1 @@
    1 |  | -XWiki.XWikiGuest
    | 1 | +xwiki:XWiki.hde

  Syntax

    ... | ... | @@ -1,1 +1,1 @@
    1 |  | -XWiki 2.1
    | 1 | +XWiki 2.0

  Content

    ... | ... | @@ -1,0 +1,150 @@
    | 1 | +XSLT Macro helps to transform XML document with XSLT style sheet.
    | 2 | +Macro is able to retrieve XML document or style sheet from attachment or URL
    | 3 | +XML document or else style sheet could be the content of the macro.
    | 4 | +
    | 5 | +|=Parameter|=Description|=Possible values|=Default value
    | 6 | +|xsl|XSLT style sheet to apply to XML|(((http~://somewhere/style.xsl
    | 7 | +attach~:~[[space.]page@]style.xsl)))| none
    | 8 | +|xml|XML document to process|(((http~://somewhere/document.xml
    | 9 | +attach~:~[[space.]page@]document.xml)))| none
    | 10 | +|params|Comma separated list of parameters name, value pair|(((name1=value1[,name2=value2[,...~]])))| none
    | 11 | +
    | 12 | +== Samples ==
    | 13 | +===xml and xsl parameters===
    | 14 | +{{code}}{{xslt xsl="attach:persons.xsl" xml="attach:persons.xml"/}}{{/code}}
    | 15 | +
    | 16 | +{{xslt xsl="attach:persons.xsl" xml="http://localhost:8080/xwiki/bin/download/Macros/xslt/persons.xml"/}}
    | 17 | +
    | 18 | +===xsl parameter and content as xml document==
    | 19 | +{{code}}{{xslt xsl="attach:persons.xsl"}}
    | 20 | +<persons>
    | 21 | + <person username="JS1">
    | 22 | + <name>John</name>
    | 23 | + <family_name>Smith</family_name>
    | 24 | + </person>
    | 25 | + <person username="ND1">
    | 26 | + <name>Nancy</name>
    | 27 | + <family_name>Davolio</family_name>
    | 28 | + </person>
    | 29 | +</persons>
    | 30 | +{{/xslt}}
    | 31 | +{{/code}}
    | 32 | +
    | 33 | +{{xslt xsl="attach:persons.xsl"}}
    | 34 | +<persons>
    | 35 | + <person username="JS1">
    | 36 | + <name>John</name>
    | 37 | + <family_name>Smith</family_name>
    | 38 | + </person>
    | 39 | + <person username="ND1">
    | 40 | + <name>Nancy</name>
    | 41 | + <family_name>Davolio</family_name>
    | 42 | + </person>
    | 43 | +</persons>
    | 44 | +{{/xslt}}
    | 45 | +
    | 46 | +===xml parameter and content as xsl style sheet==
    | 47 | +{{code}}{{xslt xml="attach:persons.xml"}}
    | 48 | +<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    | 49 | + <xsl:output method="text"/>
    | 50 | + <xsl:template match="/">
    | 51 | + <xsl:text>
    | 52 | +|=Family name|=Name
    | 53 | +</xsl:text>
    | 54 | + <xsl:apply-templates select="//person">
    | 55 | + <xsl:sort select="family_name" />
    | 56 | + </xsl:apply-templates>
    | 57 | + <xsl:text>
    | 58 | +</xsl:text>
    | 59 | + </xsl:template>
    | 60 | + <xsl:template match="person">
    | 61 | + <xsl:text>|</xsl:text>
    | 62 | + <xsl:value-of select="family_name"/>
    | 63 | + <xsl:text>|</xsl:text>
    | 64 | + <xsl:value-of select="name"/>
    | 65 | + <xsl:text>
    | 66 | +</xsl:text>
    | 67 | + </xsl:template>
    | 68 | +</xsl:stylesheet>{{/xslt}}{{/code}}
    | 69 | +
    | 70 | +{{xslt xml="attach:persons.xml"}}
    | 71 | +<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    | 72 | + <xsl:output method="text"/>
    | 73 | + <xsl:template match="/">
    | 74 | + <xsl:text>
    | 75 | +|=Family name|=Name
    | 76 | +</xsl:text>
    | 77 | + <xsl:apply-templates select="//person">
    | 78 | + <xsl:sort select="family_name" />
    | 79 | + </xsl:apply-templates>
    | 80 | + <xsl:text>
    | 81 | +</xsl:text>
    | 82 | + </xsl:template>
    | 83 | + <xsl:template match="person">
    | 84 | + <xsl:text>|</xsl:text>
    | 85 | + <xsl:value-of select="family_name"/>
    | 86 | + <xsl:text>|</xsl:text>
    | 87 | + <xsl:value-of select="name"/>
    | 88 | + <xsl:text>
    | 89 | +</xsl:text>
    | 90 | + </xsl:template>
    | 91 | +</xsl:stylesheet>{{/xslt}}
    | 92 | +
    | 93 | +===xml parameter, content as xsl style sheet and params parameter==
    | 94 | +{{code}}{{xslt xml="attach:persons.xml" params="user=JS1"}}
    | 95 | +<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    | 96 | + <xsl:output method="text"/>
    | 97 | + <xsl:param name="user"/>
    | 98 | + <xsl:template match="/">
    | 99 | + <xsl:text>
    | 100 | +|=Family name|=Name
    | 101 | +</xsl:text>
    | 102 | + <xsl:apply-templates select="//person[@username=$user]">
    | 103 | + <xsl:sort select="family_name" />
    | 104 | + </xsl:apply-templates>
    | 105 | + <xsl:text>
    | 106 | +</xsl:text>
    | 107 | + </xsl:template>
    | 108 | + <xsl:template match="person">
    | 109 | + <xsl:text>|</xsl:text>
    | 110 | + <xsl:value-of select="family_name"/>
    | 111 | + <xsl:text>|</xsl:text>
    | 112 | + <xsl:value-of select="name"/>
    | 113 | + <xsl:text>
    | 114 | +</xsl:text>
    | 115 | + </xsl:template>
    | 116 | +</xsl:stylesheet>{{/xslt}}{{/code}}
    | 117 | +
    | 118 | +{{xslt xml="attach:persons.xml" params="user=JS1"}}
    | 119 | +<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    | 120 | + <xsl:output method="text"/>
    | 121 | + <xsl:param name="user"/>
    | 122 | + <xsl:template match="/">
    | 123 | + <xsl:text>
    | 124 | +|=Family name|=Name
    | 125 | +</xsl:text>
    | 126 | + <xsl:apply-templates select="//person[@username=$user]">
    | 127 | + <xsl:sort select="family_name" />
    | 128 | + </xsl:apply-templates>
    | 129 | + <xsl:text>
    | 130 | +</xsl:text>
    | 131 | + </xsl:template>
    | 132 | + <xsl:template match="person">
    | 133 | + <xsl:text>|</xsl:text>
    | 134 | + <xsl:value-of select="family_name"/>
    | 135 | + <xsl:text>|</xsl:text>
    | 136 | + <xsl:value-of select="name"/>
    | 137 | + <xsl:text>
    | 138 | +</xsl:text>
    | 139 | + </xsl:template>
    | 140 | +</xsl:stylesheet>{{/xslt}}
    | 141 | +
    | 142 | +=== missing parameters ===
    | 143 | +{{code}}{{xslt /}}{{/code}}
    | 144 | +
    | 145 | +{{xslt /}}
    | 146 | +
    | 147 | +=== missing content===
    | 148 | +{{code}}{{xslt xsl="attach:persons.xsl"/}}{{/code}}
    | 149 | +
    | 150 | +{{xslt xsl="attach:persons.xsl"/}}

persons.xml
  Author

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +xwiki:XWiki.hde

  Size

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +0 bytes

  Content

    ... | ... | @@ -1,0 +1,11 @@
    | 1 | +<?xml version="1.0" ?>
    | 2 | +<persons>
    | 3 | + <person username="JS1">
    | 4 | + <name>John</name>
    | 5 | + <family_name>Smith</family_name>
    | 6 | + </person>
    | 7 | + <person username="ND1">
    | 8 | + <name>Nancy</name>
    | 9 | + <family_name>Davolio</family_name>
    | 10 | + </person>
    | 11 | +</persons>

persons.xsl
  Author

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +xwiki:XWiki.hde

  Size

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +0 bytes

  Content

    ... | ... | @@ -1,0 +1,22 @@
    | 1 | +<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    | 2 | + <xsl:output method="text"/>
    | 3 | + <xsl:template match="/">
    | 4 | + <xsl:text>
    | 5 | +|=Family name|=Name
    | 6 | +</xsl:text>
    | 7 | + <xsl:apply-templates select="//person">
    | 8 | + <xsl:sort select="family_name" />
    | 9 | + </xsl:apply-templates>
    | 10 | + <xsl:text>
    | 11 | +</xsl:text>
    | 12 | + </xsl:template>
    | 13 | + <xsl:template match="person">
    | 14 | + <xsl:text>|</xsl:text>
    | 15 | + <xsl:value-of select="family_name"/>
    | 16 | + <xsl:text>|</xsl:text>
    | 17 | + <xsl:value-of select="name"/>
    | 18 | + <xsl:text>
    | 19 | +</xsl:text>
    | 20 | + </xsl:template>
    | 21 | +</xsl:stylesheet>
    | 22 | +

XWiki.WikiMacroClass[0]
  Macro code

    ... | ... | @@ -1,0 +1,78 @@
    | 1 | +{{velocity}}#set($ok = $context.context.put("mydoc", $doc)){{/velocity}}{{groovy}}
    | 2 | +import javax.xml.transform.TransformerFactory
    | 3 | +import javax.xml.transform.stream.StreamResult
    | 4 | +import javax.xml.transform.stream.StreamSource
    | 5 | +
    | 6 | +def xmldoc=xcontext.mydoc
    | 7 | +def xsldoc=xcontext.mydoc
    | 8 | +def xsl=xcontext.macro.params.xsl
    | 9 | +def xml=xcontext.macro.params.xml
    | 10 | +def params=xcontext.macro.params.params
    | 11 | +
    | 12 | +if (null == xsl && null == xml) {
    | 13 | + println "\n{{error}}**xslt** macro need at least one of **xsl** or **xml** parameter{{/error}}"
    | 14 | +} else if (null == xcontext.macro.content && (null == xsl || null == xml)){
    | 15 | + println "\n{{error}}**xslt** macro content is mandatory if only one of **xsl** or **xml** parameter is set{{/error}}"
    | 16 | +} else {
    | 17 | + if (null == xml) {
    | 18 | + xml=xcontext.macro.content
    | 19 | + } else if (null == xsl) {
    | 20 | + xsl=xcontext.macro.content
    | 21 | + }
    | 22 | +
    | 23 | +//print "\n\n{{code}}"+xsl+"{{/code}}\n\n"
    | 24 | +//print "\n\n{{code}}"+xml+"{{/code}}\n\n"
    | 25 | + def sst = new StreamSource()
    | 26 | + if (null != xsl && xsl.startsWith("attach:")) {
    | 27 | + xsl=xsl.substring(7)
    | 28 | + if (xsl.contains('@')) {
    | 29 | + xsldoc=xwiki.getDocument(xsl.substring(0, xsl.indexOf('@')))
    | 30 | + xsl=xsl.substring(xsl.indexOf('@')+1)
    | 31 | + }
    | 32 | + xsl=xsldoc.getAttachment(xsl).getContentAsString()
    | 33 | + }
    | 34 | + if (xsl.startsWith("http")) {
    | 35 | +// xsl=xwiki.getURLContent(xsl)
    | 36 | + sst.setSystemId(xsl)
    | 37 | + } else {
    | 38 | + sst.setReader(new StringReader(xsl))
    | 39 | + }
    | 40 | +
    | 41 | + def ssx = new StreamSource()
    | 42 | + if (null != xml && xml.startsWith("attach:")) {
    | 43 | + xml=xml.substring(7)
    | 44 | + if (xml.contains('@')) {
    | 45 | + xmldoc=xwiki.getDocument(xml.substring(0, xml.indexOf('@')))
    | 46 | + xml=xml.substring(xml.indexOf('@')+1)
    | 47 | + }
    | 48 | + xml=xmldoc.getAttachment(xml).getContentAsString()
    | 49 | + }
    | 50 | + if (xml.startsWith("http")) {
    | 51 | +// xml = xwiki.getURLContent(xml)
    | 52 | + ssx.setSystemId(xml)
    | 53 | + } else {
    | 54 | + ssx.setReader(new StringReader(xml))
    | 55 | + }
    | 56 | +
    | 57 | +//print "\n\n{{code}}"+xsl+"{{/code}}\n\n"
    | 58 | +//print "\n\n{{code}}"+xml+"{{/code}}\n\n"
    | 59 | +
    | 60 | + def factory = TransformerFactory.newInstance()
    | 61 | +// def transformer = factory.newTransformer(new StreamSource(new StringReader(xsl)))
    | 62 | + def transformer = factory.newTransformer(sst)
    | 63 | + if (null != params) {
    | 64 | +//println params
    | 65 | + params.split("(?!\\\\),").each {
    | 66 | +//print "it "
    | 67 | + def param=it.split("(?!\\\\)=")
    | 68 | + transformer.setParameter(param[0], param[1])
    | 69 | +//println param[0]+"="+param[1]
    | 70 | + }
    | 71 | + }
    | 72 | + def StringWriter sw = new StringWriter()
    | 73 | +// transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(sw))
    | 74 | + transformer.transform(ssx, new StreamResult(sw))
    | 75 | + println sw
    | 76 | +}
    | 77 | +
    | 78 | +{{/groovy}}

  Content description (Not applicable for "No content" type)

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +XML document or else XSLT style-sheet

  Macro content type

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +Optional

  Macro description

    ... | ... | @@ -1,0 +1,4 @@
    | 1 | +XSLT Macro help to transform XML document with XSLT style-sheet.
    | 2 | +Macro is able to retrieve XML document or style sheet from attachment or URL
    | 3 | +XML document or else style sheet could be the content of the macro.
    | 4 | +Parameters could be pass to the XSLT style-sheet.

  Macro id

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +xslt

  Macro name

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +xslt

  Supports inline mode

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +Yes

  Macro visibility

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +Global

XWiki.WikiMacroParameterClass[0]
  Parameter description

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +XSLT style sheet URL http: or attach:

  Parameter mandatory

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +No

  Parameter name

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +xsl

XWiki.WikiMacroParameterClass[1]
  Parameter description

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +XML document URL http: or attach:

  Parameter mandatory

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +No

  Parameter name

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +xml

XWiki.WikiMacroParameterClass[2]
  Parameter description

    ... | ... | @@ -1,0 +1,2 @@
    | 1 | +Parameters for XSLT style-sheet: xsm:param elements.
    | 2 | +It is comma separated name=value pair: firstname=John,age=40

  Parameter mandatory

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +No

  Parameter name

    ... | ... | @@ -1,0 +1,1 @@
    | 1 | +params

# Applications

# Tips

If you're starting with XWiki, check out the Getting Started Guide.

# Need help?

If you need help with XWiki you can contact:

- Community Support
- Professional Support