Skip to content

Instantly share code, notes, and snippets.

@jelovirt
Last active October 9, 2016 17:13
Show Gist options
  • Save jelovirt/cd100d71f723fd3e25818baa0b2c54ea to your computer and use it in GitHub Desktop.
Save jelovirt/cd100d71f723fd3e25818baa0b2c54ea to your computer and use it in GitHub Desktop.
Inline matching in pseudo-XSLT
<xsl:template match="metasyntactic">
<xsl:match select=".">
<xsl:case match="foo">foo</xsl:case>
<xsl:case match="bar">bar</xsl:case>
<xsl:case match="baz">baz</xsl:case>
</xsl:match>
</xsl:template>
@eerohele
Copy link

eerohele commented Oct 9, 2016

I don't know anything about Schematron Quick Fix, so I don't really know why you'd use it here. Couldn't you just use XSLT?

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:local="local"
  exclude-result-prefixes="local xs"
  version="3.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:namespace-alias result-prefix="xsl" stylesheet-prefix="local"/>

  <xsl:template match="xsl:template[xsl:match]">
    <xsl:variable name="mode" select="generate-id(xsl:match)" as="xs:string"/>

    <xsl:next-match>
      <xsl:with-param name="mode" select="$mode" as="xs:string" tunnel="yes"/>
    </xsl:next-match>

    <xsl:apply-templates select="xsl:match/xsl:case">
      <xsl:with-param name="mode" select="$mode" as="xs:string" tunnel="yes"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="xsl:match">
    <xsl:param name="mode" as="xs:string" tunnel="yes"/>

    <local:apply-templates>
      <xsl:attribute name="mode" select="$mode"/>
      <xsl:apply-templates select="@select"/>
    </local:apply-templates>
  </xsl:template>

  <xsl:template match="xsl:case">
    <xsl:param name="mode" as="xs:string" tunnel="yes"/>

    <local:template mode="{$mode}">
      <xsl:sequence select="node()"/>
    </local:template>
  </xsl:template>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

But yeah, this kind of thing should really be baked into the language itself, since debugging generated stylesheets isn't a whole lot of fun.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment