Skip to content

Commit

Permalink
Addressing usnistgov#212 - XML to JSON converter no longer chokes on …
Browse files Browse the repository at this point in the history
…edge cases (array items of SINGLETON_OR_ARRAY groups)
  • Loading branch information
wendellpiez authored and david-waltermire committed Jul 14, 2022
1 parent 6fe6b3a commit 68248c0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
21 changes: 12 additions & 9 deletions toolchains/xslt-M4/converter-gen/supermodel-to-json.xsl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?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"
xpath-default-namespace="http://csrc.nist.gov/ns/oscal/metaschema/1.0/supermodel"
xmlns="http://www.w3.org/2005/xpath-functions"
exclude-result-prefixes="xs math"
exclude-result-prefixes="#all"
default-mode="write-json"
version="3.0">

Expand Down Expand Up @@ -138,13 +137,17 @@
<xsl:variable name="key-flag-name" select="@key-flag"/>
<xsl:element name="{(@in-json[matches(.,'\S')],'string')[1]}"
namespace="http://www.w3.org/2005/xpath-functions">
<xsl:copy-of
select="((../flag[@key=$key-flag-name],parent::field[@in-json = 'SCALAR'])/@key, @key)[1]"/>
<!-- overriding the key -->
<xsl:if test="exists(@key-flag)">
<xsl:attribute name="key">
<xsl:apply-templates select="../flag[@name=$key-flag-name]" mode="cast-data"/>
</xsl:attribute>
<!-- emit a key only if needed -->
<xsl:if test="exists(parent::field/child::flag) or exists(parent::field/parent::assembly) or count(parent::field/parent::group[@in-json='SINGLETON_OR_ARRAY']/child::*) eq 1">
<xsl:copy-of
select="((../flag[@key = $key-flag-name], parent::field[@in-json = 'SCALAR'])/@key, @key)[1]"/>
<!-- overriding the key -->
<xsl:if test="exists(@key-flag)">
<xsl:attribute name="key">
<xsl:apply-templates select="../flag[@name = $key-flag-name]"
mode="cast-data"/>
</xsl:attribute>
</xsl:if>
</xsl:if>
<xsl:apply-templates select="." mode="cast-data"/>
</xsl:element>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@

<xsl:variable name="metaschema-source" select="/"/>

<xsl:variable name="json-serializer-xslt" select="document('converter-gen/supermodel-to-json.xsl')"/>

<xsl:template match="/">
<xsl:variable name="converter">
<xsl:call-template name="nm:process-pipeline">
Expand Down Expand Up @@ -117,7 +119,7 @@
<xsl:text>&#xA;</xsl:text>
<xsl:comment> XML to JSON conversion: Supermodel serialization as JSON
including markdown production </xsl:comment>
<xsl:apply-templates mode="package-converter" select="document('converter-gen/supermodel-to-json.xsl')/xsl:*/( xsl:variable | xsl:template )"/>
<xsl:apply-templates mode="package-converter" select="$json-serializer-xslt/xsl:*/( xsl:variable | xsl:template )"/>
</xsl:copy>
</xsl:template>

Expand Down

0 comments on commit 68248c0

Please sign in to comment.