Skip to content

Instantly share code, notes, and snippets.

@magwas
Last active December 7, 2019 21:14
Show Gist options
  • Save magwas/01137443e6690b3231011364d2cea568 to your computer and use it in GitHub Desktop.
Save magwas/01137443e6690b3231011364d2cea568 to your computer and use it in GitHub Desktop.
an XSLT challenge
Please consider the following output. It was created by running an xslt script on itself.
You can easily reconstruct most of the script from this output. Most of it.
Level 0: reconstruct the script
Level 1: modify the script such that all of the script is reconstructable from its output
Level 1a: modify the script such that the output is "better" in some ways. E.g. ';' instead of '{}',
omission of things like 'select=', 'name=', 'test=' where it is unambigous, use 'let' or 'var' instead of xsl:variable,
'/"<pattern>"/' instead of 'xsl:template(match="<pattern>")', etc.
Level 2: modify the script such that it emits all xml constructs in a reconstructable way (e.g. processing instructions)
Level 3: write something which actually reconstructs the original xml
Level 4: write the reconstructing logic in xslt
-------------------
xsl:stylesheet(version = "2.0") {
xsl:output(method = "text", encoding = "utf-8", omit-xml-declaration = "yes") {
}
xsl:strip-space(elements = "*") {
}
xsl:function(name = "zenta:fill") {
xsl:param(name = "depth") {
}
xsl:value-of(select = " string-join( for $i in (0 to $depth) return ' ', '' )") {
}
}
xsl:variable(name = "example") {
head() {
title() {
}
link(rel = "stylesheet", type = "text/css", href = "structured.css") {
}
meta(name = "generator", content = "DocBook XSL Stylesheets V1.79.1") {
}
}
}
xsl:template(match = "*") {
xsl:param(name = "depth", tunnel = "yes", select = "0") {
}
xsl:variable(name = "params") {
xsl:apply-templates(select = "@*") {
}
}
xsl:value-of(select = "concat( zenta:fill($depth), name(), '(', string-join($params/*,', '), ') {
')") {
}
/* This is
a comment */
xsl:apply-templates(select = "*|text()|processing-instruction()|comment()") {
xsl:with-param(name = "depth", select = "$depth+1", tunnel = "yes") {
}
}
xsl:value-of(select = "concat( zenta:fill($depth), '}
')") {
}
}
xsl:template(match = "@*") {
a() {
xsl:value-of(select = "concat(name(),' = "',.,'"')") {
}
}
}
xsl:template(match = "text()") {
xsl:value-of(select = ".") {
}
}
xsl:template(match = "comment()") {
xsl:param(name = "depth", tunnel = "yes", select = "0") {
}
xsl:value-of(select = "concat( zenta:fill($depth), '/*', ., '*/
' )") {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment