Skip to content

Commit

Permalink
Implementing shell logic for producing standalone converter XSLTs fro…
Browse files Browse the repository at this point in the history
…m metaschema source. Many bug repairs and extensions. #53.
  • Loading branch information
wendellpiez authored and david-waltermire committed Dec 21, 2020
1 parent b984362 commit 9cbc2b0
Show file tree
Hide file tree
Showing 14 changed files with 937 additions and 571 deletions.
128 changes: 0 additions & 128 deletions toolchains/xslt-M4/converter-gen/MAKE-JSON-TO-XML-CONVERTER.xsl

This file was deleted.

61 changes: 0 additions & 61 deletions toolchains/xslt-M4/converter-gen/MAKE-XML-TO-JSON-CONVERTER.xsl

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
<xsl:mode on-no-match="shallow-copy"/>

<xsl:template match="value[@as-type=('markup-line','markup-multiline')]">
<xsl:call-template name="parse">
<xsl:call-template name="parse-markdown">
<xsl:with-param name="markdown-str" select="string(.)"/>
</xsl:call-template>
</xsl:template>

<xsl:template name="run-tests" match="/">
<xsl:template name="run-tests">
<!--<xsl:copy-of select="$tag-replacements"/>-->
<!--<xsl:copy-of select="$examples"/>-->
<xsl:call-template name="parse">
<xsl:call-template name="parse-markdown">
<xsl:with-param name="markdown-str" select="string($examples)"/>
</xsl:call-template>
<!--<xsl:for-each select="$line-example">
Expand Down Expand Up @@ -59,7 +59,7 @@
(we can ignore quotes as long as our markup has no attributes only elements)
-->

<xsl:template name="parse">
<xsl:template name="parse-markdown">

<!-- First, group according to ``` delimiters btw codeblocks and not
within codeblock, escape & and < (only)
Expand Down
14 changes: 7 additions & 7 deletions toolchains/xslt-M4/converter-gen/produce-json-converter.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@
<xsl:apply-templates select="." mode="make-json-pull"/>
</xsl:template>



<xsl:template priority="2" match="value" mode="make-json-pull">
<XSLT:apply-templates select="." mode="get-value-property"/>
</xsl:template>
Expand All @@ -90,8 +88,10 @@
<XSLT:apply-templates select="*[@key='{@key}']"/>
</xsl:template>



<xsl:template match="group/*[empty(@key)]" mode="make-json-pull">
<XSLT:apply-templates select="*"/>
</xsl:template>

<!-- overriding template in produce-xml-converter that suppresses template
production for an element not present in the XML: this time we want the field
(but must also hard-wire the match). -->
Expand Down Expand Up @@ -141,7 +141,7 @@
<xsl:apply-templates select="." mode="make-match"/>
</xsl:variable>
<!-- now producing a template to produce a value node representing the value of the field-->
<XSLT:template match="{ $matching }" mode="get-value-property">
<XSLT:template match="{ $matching }" mode="get-value-property" priority="{count(ancestor-or-self::*)}">
<value>
<xsl:copy-of select="value/(@key | @key-flag | @as-type)"/>
<xsl:apply-templates select="value/@as-type" mode="assign-json-type"/>
Expand All @@ -160,8 +160,8 @@
<xsl:variable name="matching">
<xsl:apply-templates select="." mode="make-match"/>
</xsl:variable>
<XSLT:template match="{ $matching}" mode="keep-value-property">
<xsl:comment> Property is a flag; dropped when grabbing values</xsl:comment>
<XSLT:template match="{ $matching}" mode="keep-value-property" priority="{count(ancestor-or-self::*)}">
<xsl:comment> Not keeping the flag here. </xsl:comment>
</XSLT:template>
</xsl:template>

Expand Down
40 changes: 26 additions & 14 deletions toolchains/xslt-M4/converter-gen/produce-xml-converter.xsl
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:m="http://csrc.nist.gov/ns/oscal/metaschema/1.0"
xmlns:XSLT="http://csrc.nist.gov/ns/oscal/metaschema/xslt-alias"

xpath-default-namespace="http://csrc.nist.gov/ns/oscal/metaschema/1.0"
exclude-result-prefixes="xs math"
version="3.0"
xmlns="http://csrc.nist.gov/ns/oscal/metaschema/1.0/supermodel">

Expand Down Expand Up @@ -54,8 +52,9 @@
<!-\-<XSLT:output-character character="&#xE0000;" string="\"/>-\->
</XSLT:character-map>-->

<!-- first we produce templates for (each) of the global definitions.-->
<xsl:for-each-group select="//*[@scope = 'global'][not(@recursive='true')]"
<!-- first we produce templates for (each) of the global definitions
and for any flags thereon.-->
<xsl:for-each-group select="//*[@scope = 'global'][not(@recursive='true')]/( . | child::flag )"
group-by="string-join((local-name(), @name), ':')">
<!-- These are all the same so we do only one, but we pass in the group to construct the match -->
<xsl:apply-templates select="current-group()[1]" mode="make-template">
Expand Down Expand Up @@ -110,12 +109,12 @@
</xsl:if>
</xsl:template>

<xsl:template match="*[@scope='global']" mode="make-template-for-local"/>
<xsl:template match="*[@scope='global']" mode="make-template-for-local"/>

<xsl:template priority="1" match="*[@scope='global']/flag" mode="make-template-for-local"/>

<xsl:template match="*" mode="make-template-for-local">
<xsl:apply-templates select="." mode="make-template">
<xsl:with-param name="local" select="true()"/>
</xsl:apply-templates>
<xsl:apply-templates select="." mode="make-template"/>
</xsl:template>

<!-- no template for implicit wrappers on markup-multiline -->
Expand All @@ -129,7 +128,7 @@
<xsl:variable name="json-key-flag-name" select="@json-key-flag"/>
<XSLT:template match="{ $matching}">
<xsl:if test="not(@scope='global')">
<xsl:attribute name="priority" select="10"/>
<xsl:attribute name="priority" select="count(ancestor-or-self::*)"/>
</xsl:if>
<!-- A parameter allows the call to drop the key, necessary for recursive
groups of elements also allowed at the root (at least) -->
Expand Down Expand Up @@ -163,7 +162,9 @@
<xsl:apply-templates select="." mode="make-match"/>
</xsl:variable>
<XSLT:template match="{ $matching}">
<xsl:call-template name="comment-template"/>
<xsl:if test="not((.|..)/@scope='global')">
<xsl:attribute name="priority" select="count(ancestor-or-self::*)"/>
</xsl:if><xsl:call-template name="comment-template"/>
<flag in-json="string">
<xsl:copy-of select="@* except @scope"/>
<!-- rewriting in-json where necessary -->
Expand Down Expand Up @@ -204,10 +205,13 @@

<!-- Matches local declarations (only) -->
<xsl:template match="*" mode="make-xml-match">
<xsl:for-each select="ancestor-or-self::*[@gi] except ancestor-or-self::*[@scope='global']/ancestor::*">
<xsl:if test="position() gt 1">/</xsl:if>
<xsl:apply-templates select="." mode="make-xml-step"/>
</xsl:for-each>
<xsl:value-of>
<xsl:for-each
select="ancestor-or-self::*[@gi]">
<xsl:if test="position() gt 1">/</xsl:if>
<xsl:apply-templates select="." mode="make-xml-step"/>
</xsl:for-each>
</xsl:value-of>
</xsl:template>

<!-- fallback should never match -->
Expand All @@ -217,6 +221,14 @@
</pull>
</xsl:template>

<xsl:template match="choice" mode="make-xml-pull make-json-pull">
<xsl:apply-templates mode="#current"/>
</xsl:template>

<xsl:template match="constraint" mode="make-xml-pull make-json-pull">
<!-- <xsl:copy-of select="."/>-->
</xsl:template>

<xsl:variable name="prose-elements">p | ul | ol | pre | h1 | h2 | h3 | h4 | h5 | h6 | table</xsl:variable>

<!-- A field without a GI is implicit in the XML; Metaschema prevents it from having flags -->
Expand Down
Loading

0 comments on commit 9cbc2b0

Please sign in to comment.