Skip to content

Commit

Permalink
More fixes. Mainly
Browse files Browse the repository at this point in the history
I re-worked a large for-each to use apply-templates. Much
better. Also added code to handle queryBinding=.
  • Loading branch information
sydb committed Jan 20, 2025
1 parent 2ff142b commit 4ca8699
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 102 deletions.
101 changes: 29 additions & 72 deletions odds/extract-isosch.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -358,53 +358,12 @@ of this software, even if advised of the possibility of such damage.

<xsl:if test="key('CONSTRAINTs', $langs )[ not( self::sch:ns ) ]">
<xsl:variable name="N"
select="', of which there are '||count( key('CONSTRAINTs', $langs, $root )/*[ not( self::sch:ns ) ] )"/>
select="', of which there are '||count( key('CONSTRAINTs', $langs, $root )/*[ not( self::sch:ns ) ] )"/>
<xsl:call-template name="blockComment">
<xsl:with-param name="content" select="'constraints in '||string-join( $langs, ', ')||$N"/>
</xsl:call-template>
</xsl:if>
<xsl:for-each select="$root/key('CONSTRAINTs', $langs )[ not( self::sch:ns ) ]">
<xsl:variable name="patID" select="tei:makePatternID(.)"/>
<xsl:message select="'DEBUG: processing '||$patID"/>
<xsl:choose>
<xsl:when test="sch:pattern">
<xsl:message select="'debug: a pattern child'"/>
<!-- IF there is a child <pattern>, we just copy over all children, no tweaking -->
<xsl:apply-templates select="node()">
<!-- they all get handed $patID, but only the template for 'pattern' uses it -->
<xsl:with-param name="patID" select="$patID"/>
</xsl:apply-templates>
</xsl:when>
<xsl:when test="sch:rule">
<xsl:message select="'debug: a rule child'"/>
<!-- IF there is no <pattern>, but there is a <rule>, copy over all children -->
<!-- into a newly created <pattern> wrapper -->
<pattern id="{$patID}">
<xsl:apply-templates select="node()"/>
</pattern>
</xsl:when>
<xsl:when test="sch:assert | sch:report | sch:extends">
<xsl:message select="'debug: some other child'"/>
<!-- IF there is no <pattern> nor <rule> child, but there is a child that -->
<!-- requires being wrapped in a rule, create both <rule> and <pattern> -->
<!-- wrappers for them, making HERE the context. NOTE: As of 2025-03-15 -->
<!-- a free-floating <assert> or <report> without a parent <rule> (with a -->
<!-- @context attribute) will no longer be allowed, so this entire <when> -->
<!-- could maybe be dropped. See PR TEI #2513. -->
<pattern id="{$patID}">
<rule context="{tei:generate-context(.)}">
<xsl:apply-templates select="node()"/>
</rule>
</pattern>
</xsl:when>
<xsl:otherwise>
<xsl:message select="'debug: OTHERWISE'"/>
<!-- IF there is neither a <pattern> nor a <rule>, nor a child that would -->
<!-- require being wrapped in those, just copy over whatever we have -->
<xsl:apply-templates select="node()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates select="$root/key('CONSTRAINTs', $langs )[ not( self::sch:ns ) ]"/>

<xsl:if test="key('DEPRECATEDs',1)">
<xsl:call-template name="blockComment">
Expand Down Expand Up @@ -501,17 +460,28 @@ of this software, even if advised of the possibility of such damage.

</schema>
</xsl:template>
<xsl:template match="tei:constraint/sch:rule">
<rule>
<xsl:apply-templates select="@*"/>
<xsl:if test="not(@context)">
<!-- note: don't want to call generate-context() if not needed, -->
<!-- as we may want it to generate warning msgs -->
<xsl:attribute name="context" select="tei:generate-context(.)"/>
</xsl:if>

<xsl:template match="tei:constraint">
<xsl:apply-templates select="node()"/>
</xsl:template>

<xsl:template match="tei:constraint/sch:pattern">
<xsl:variable name="patID" select="tei:makePatternID( child::sch:pattern[1] )"/>
<!-- (Note that makePatternID() will use the @id of <pattern>, if there is one.) -->
<pattern id="{$patID}">
<xsl:apply-templates select="node()"/>
</rule>
</pattern>
</xsl:template>

<xsl:template match="tei:constraint/sch:rule|tei:constraintDecl/sch:rule">
<xsl:variable name="patID" select="tei:makePatternID(.)"/>
<!-- IF there is no <pattern>, but there is a <rule>, copy over all children -->
<!-- into a newly created <pattern> wrapper -->
<pattern id="{$patID}">
<rule>
<xsl:apply-templates select="@*|node()"/>
</rule>
</pattern>
</xsl:template>

<xsl:template match="sch:*|xsl:*">
Expand Down Expand Up @@ -595,28 +565,15 @@ of this software, even if advised of the possibility of such damage.
<xsl:param name="context"/>
<xsl:variable name="scheme" select="$context/ancestor-or-self::constraintSpec/@scheme|$context/ancestor-or-self::constraintDecl/@scheme"/>
<xsl:for-each select="$context">
<xsl:variable name="id" select="( ancestor-or-self::*[@ident][1]/@ident,
ancestor-or-self::*[@id][1]/@id,
ancestor-or-self::*[@xml:id][1]/@xml:id,
ancestor-or-self::constraintDecl!local-name(.)
)[1]!translate( .,':','')"/>
<xsl:variable name="num">
<xsl:number level="any"/>
</xsl:variable>
<xsl:variable name="id">
<xsl:choose>
<xsl:when test="ancestor-or-self::*[@ident]">
<xsl:sequence select="ancestor-or-self::*[@ident][1]/@ident!translate( .,':','')"/>
</xsl:when>
<xsl:when test="ancestor-or-self::*[@xml:id]">
<xsl:sequence select="ancestor-or-self::*[@xml:id][1]/@xml:id!translate( .,':','')"/>
</xsl:when>
<xsl:when test="ancestor-or-self::constraintDecl">
<xsl:sequence select="'constraintDecl'"/>
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:value-of
select="( $scheme,
'constraint',
$id,
$num )"
separator="-"/>
<xsl:value-of select="( $scheme, 'constraint', $id, $num )" separator="-"/>
</xsl:for-each>
</xsl:function>

Expand Down
79 changes: 49 additions & 30 deletions odds/odd2odd.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -543,34 +543,53 @@ of this software, even if advised of the possibility of such damage.
<xsl:variable name="source" as="document-node()" select="document( tei:workOutSource( $this_schemaSpec ) )"/>
<!-- Assemble a set of all the relevant <constraintDecl>s: -->
<xsl:variable name="constraintDecls" as="element(tei:constraintDecl)*"
select="(
$source//tei:constraintDecl,
$top//tei:teiHeader//tei:constraintDecl,
$this_schemaSpec//tei:constraintDecl
)"/>
select="(
$source//tei:constraintDecl,
$top//tei:teiHeader//tei:constraintDecl,
$this_schemaSpec//tei:constraintDecl
)"/>
<!--
If there already is an <encodingDesc> (in which case we need
to copy it over) OR there are one or more <constraintDecl>s
whose contents we need to preserve for future use …
If there already is an <encodingDesc> (in which case we need
to copy it over) OR there are one or more <constraintDecl>s
whose contents we need to preserve for future use …
-->
<xsl:if test="child::tei:encodingDesc or count( $constraintDecls ) gt 0">
<!-- … output an <encodingDesc> … -->
<tei:encodingDesc>
<!-- … with any attributes the original <encodingDesc> (if any) had … -->
<xsl:apply-templates select="tei:encodingDesc/@*" mode="#current"/>
<!-- … and any content the original <encodingDesc> (if any) had; -->
<xsl:apply-templates select="tei:encodingDesc/node() except //tei:constraintDecl" mode="pass0"/>
<!-- … then get the list of schemes to which the <constraintDecl>s apply … -->
<xsl:variable name="constraintDecl_schemes" select="$constraintDecls/@scheme" as="xs:string*"/>
<!-- … and for each such (unique) scheme … -->
<xsl:for-each select="distinct-values( $constraintDecl_schemes )">
<xsl:variable name="this_scheme" select="."/>
<!-- … create an output <constraintDecl> here in the <encodingDesc> … -->
<tei:constraintDecl scheme="{$this_scheme}">
<!-- … that has the contents of *all* the applicable <constraintDecl>s for this scheme … -->
<!-- … with any attributes the original <encodingDesc> (if any) had … -->
<xsl:apply-templates select="tei:encodingDesc/@*" mode="#current"/>
<!-- … and any content the original <encodingDesc> (if any) had; -->
<xsl:apply-templates select="tei:encodingDesc/node() except //tei:constraintDecl" mode="pass0"/>
<!-- … then get the list of schemes to which the <constraintDecl>s apply … -->
<xsl:variable name="constraintDecl_schemes" select="$constraintDecls/@scheme" as="xs:string*"/>
<!-- … and for each such (unique) scheme … -->
<xsl:for-each select="distinct-values( $constraintDecl_schemes )">
<xsl:variable name="this_scheme" select="."/>
<!-- … create an output <constraintDecl> here in the <encodingDesc> … -->
<tei:constraintDecl scheme="{$this_scheme}">
<xsl:attribute name="queryBinding">
<xsl:variable name="queryBindings" as="xs:string*"
select="$constraintDecls[ @scheme eq $this_scheme ]/@queryBinding!normalize-space()"/>
<xsl:variable name="distinct-queryBindings" as="xs:string*"
select="distinct-values( $queryBindings )"/>
<xsl:choose>
<xsl:when test="count( $distinct-queryBindings ) eq 0">xslt2</xsl:when>
<xsl:when test="count( $distinct-queryBindings ) eq 1"><xsl:sequence select="$distinct-queryBindings"/></xsl:when>
<xsl:otherwise>
<xsl:variable name="queryBinding" select="($constraintDecls[ @scheme eq $this_scheme ]/@queryBinding)[last()]"/>
<xsl:message expand-text="yes">Warning: multiple
query bindings for {$this_scheme} constraints
specified in input ODDs (<xsl:value-of
select="$distinct-queryBindings" separator=", "/>).
Output combined ODD will specify {$queryBinding}.</xsl:message>
<xsl:sequence select="$queryBinding"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<!-- … that has the contents of *all* the applicable <constraintDecl>s for this scheme … -->
<xsl:apply-templates select="$constraintDecls[ @scheme eq $this_scheme ]/node()" mode="pass0"/>
</tei:constraintDecl>
</xsl:for-each>
</tei:constraintDecl>
</xsl:for-each>
</tei:encodingDesc>
</xsl:if>
</xsl:template>
Expand Down Expand Up @@ -643,9 +662,9 @@ of this software, even if advised of the possibility of such damage.
</xsl:template>

<xsl:template match=" tei:elementSpec[@mode eq 'delete']
|tei:classSpec[@mode eq 'delete']
|tei:macroSpec[@mode eq 'delete']
|tei:dataSpec[@mode eq 'delete']"
|tei:classSpec[@mode eq 'delete']
|tei:macroSpec[@mode eq 'delete']
|tei:dataSpec[@mode eq 'delete']"
mode="pass1">
<xsl:if test="$verbose='true'">
<xsl:message>Phase 1: remove <xsl:value-of select="@ident"/></xsl:message>
Expand Down Expand Up @@ -1697,14 +1716,14 @@ of this software, even if advised of the possibility of such damage.
<xsl:if test="$ORIGINAL/self::tei:elementSpec">
<xsl:apply-templates mode="justcopy"
select="tei:attList/tei:attDef
[
( @mode eq 'change'
[
( @mode eq 'change'
or @mode eq 'delete'
or @mode eq 'replace'
)
and
)
and
not( @ident = $ORIGINAL/tei:attList//tei:attDef/@ident )
]"/>
]"/>
</xsl:if>
<!-- any direct attRef elements -->
<xsl:apply-templates mode="justcopy"
Expand Down

0 comments on commit 4ca8699

Please sign in to comment.