Regis May
Regis May

Reputation: 3466

How can I pass raw XML data to a MediaWiki template?

This is how you can display source code in MediaWiki:

<syntaxhighlight lang="xml" line>
    <abc x="123" />
</syntaxhighlight>

In this example "<abc x="123"/>" will be rendered properly. But what if I would like to pass this XML snipplet "<abc x="123"/>" to some arbitrary template which in turn will pass this content to the syntaxhighlight extension? How can this be done? Standard approaches do not seem to work.


What I've figured out already:

Do you have any ideas or even know a solution to this problem?


Update: I did a fresh installation of MW 1.34.3 in order to have it as identical as possible to people trying to help me here on stackoverflow. Now:

The template named SVGExample:

{{#tag:syntaxhighlight|{{{1|}}}|lang="xml"|line=1}}

The contents of the wiki page:

{{SVGExample|<nowiki><abc x="123"/></nowiki>}}

Result:

1   &lt;abc x="123"/&gt;

It does not seem to be a matter of MediaWiki extensions. I deactivated all extensions except MagicNoCache, SyntaxHighlight_GeSHi, ParserFunctions and I got the same result.


Update for those that want to dive deeper into this:

It seems that resolving the placeholders introduces the problem here. Even with the suggested way of passing data to arguments there is some processing: Comments are filtered out. And my experiments in custom extensions indicate that as soon as you resolve the arguments passed to the template the data gets HTML encoded thus resulting in unwanted &lt; entities (and other entities). The SyntaxHighlight extension does some kind of magic to avoid this phenomenon as it receives the raw data after resolving the template arguments where a classic approach with $parser->recursiveTagParse(...) fails in this regard.

Upvotes: 0

Views: 160

Answers (1)

Alexander Mashin
Alexander Mashin

Reputation: 4637

Create a template, say, Template:Xml:

<includeonly>{{#tag:syntaxhighlight|{{{1|}}}|lang="xml"|line=1}}</includeonly>

Invoke this template like this: {{xml|1=<abc x="123" />}}. Note |1=.

Alternatively, create Template:= as = and invoke {{xml|<abc x{{=}}"123" />}}, escaping each equal sign: = → {{=}}.

The equal signs in anonymous parametres of parser functions need escaping, so that MediaWiki does not take it for a separator between parametre name and value.

Therefore, another alternative is to declare Template:xml with a named parametre, as follows:

<includeonly>{{#tag:syntaxhighlight|{{{xml|}}}|lang="xml"|line=1}}</includeonly>

and call it like this: {{xml|xml=<abc x="123" />}}.

Upvotes: 1

Related Questions