Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Add support for localization in Schematron based on diagnostics #40. #63

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions trunk/schematron/code/iso_schematron_message_xslt2.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ THE SOFTWARE.
xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias">

<xsl:import href="iso_schematron_skeleton_for_saxon.xsl"/>

<!-- Support to generate message in multiple languages -->
<xsl:import href="iso_schematron_multilingual_message_xslt2.xsl"/>

<xsl:template name="process-prolog">
<axsl:output method="text" />
Expand All @@ -51,14 +54,18 @@ THE SOFTWARE.
<!-- use default rule for process-assert and process-report:
call process-message -->

<xsl:template name="process-message">
<xsl:param name="pattern" />
<xsl:param name="role" />
<axsl:message>
<xsl:apply-templates mode="text"
/> (<xsl:value-of select="$pattern" />
<xsl:if test="$role"> / <xsl:value-of select="$role" />
</xsl:if>)</axsl:message>
</xsl:template>

<xsl:template name="process-message">
<xsl:param name="pattern" />
<xsl:param name="role" />
<xsl:param name="diagnostics" />
<axsl:message>
<xsl:call-template name="generateTextMassage">
<xsl:with-param name="diagnostics" select="$diagnostics"/>
<xsl:with-param name="currentLanguage" select="$langCode"/>
</xsl:call-template>

(<xsl:value-of select="$pattern" />
<xsl:if test="$role"> / <xsl:value-of select="$role" />
</xsl:if>)</axsl:message>
</xsl:template>
</xsl:stylesheet>
166 changes: 166 additions & 0 deletions trunk/schematron/code/iso_schematron_multilingual_message_xslt2.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?xml version="1.0" ?><?xar XSLT?>
<!-- Implementation for the Schematron XML Schema Language.
Generates simple text output messages using XSLT2 engine
-->
<!--
Open Source Initiative OSI - The MIT License:Licensing
[OSI Approved License]

This source code was previously available under the zlib/libpng license.
Attribution is polite.

The MIT License

Copyright (c) 2000-2010 Rick Jellife and Academia Sinica Computing Centre, Taiwan.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<!--
Schematron multilingual messages

Annex G of the ISO Schematron specification defines the use of multilingual Schematron as follows:
Diagnostics in multiple languages may be supported by using a different diagnostic element for each language,
with the appropriate xml:lang language attribute, and referencing all the unique identifiers of the diagnostic
elements in the diagnostics attribute of the assertion.

The language of the messages is controlled by the $langCode global parameter.
-->

<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias"
xmlns:iso="http://purl.oclc.org/dsdl/schematron"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<!-- The diagnostics elements -->
<xsl:key name="diag" match="iso:diagnostic" use="@id"/>

<!-- Generate the message depending on the language
- if there are messages in the language specified by the $currentLanguage parameter, the messages in that language will be presented
- otherwise, all messages will be presented, prefixed with language code
-->
<xsl:template name="generateTextMassage">
<xsl:param name="diagnostics"/>
<xsl:param name="currentLanguage" required="yes"/>

<!-- Get all localization (diagnostics) nodes. -->
<xsl:variable name="localizationNodes" as="item()*">
<xsl:if test="$diagnostics != ''">
<xsl:variable name="assert" select="."/>
<xsl:for-each select="tokenize($diagnostics, ' ')">
<xsl:sequence select="key('diag', current(), root($assert))"/>
</xsl:for-each>
</xsl:if>
</xsl:variable>

<!-- Generate the (localization) diagnostics messages for the current language-->
<xsl:variable name="localizationMessages" as="item()*">
<xsl:for-each select="$localizationNodes">
<xsl:call-template name="getMessage">
<xsl:with-param name="reqLang" select="$currentLanguage"/>
<xsl:with-param name="isAddId" select="false()"/>
</xsl:call-template>
</xsl:for-each>
</xsl:variable>

<!-- Generate the message from assert only if matches the current language -->
<xsl:variable name="assertMsg" as="item()*">
<xsl:call-template name="getMessage">
<xsl:with-param name="reqLang" select="$currentLanguage"/>
</xsl:call-template>
</xsl:variable>

<xsl:choose>
<xsl:when
test="not(empty($localizationMessages)) or (not(empty($assertMsg)) and $localizationNodes)">
<!-- Generate the message for the current language, both from the assertion and from localization nodes -->
<xsl:sequence select="$assertMsg"/>
<xsl:sequence select="$localizationMessages"/>
</xsl:when>
<xsl:when test="$currentLanguage != 'default'">
<!-- If no localization (diagnostics) for a specific language-->
<xsl:choose>
<!-- Generate the assertion message for the current language-->
<xsl:when test="not(empty($assertMsg))">
<xsl:sequence select="$assertMsg"/>
</xsl:when>
<xsl:otherwise>
<!-- Generate the messages for all languages. -->
<!-- Print assertion message -->
<xsl:variable name="assertMsgDef">
<xsl:call-template name="getMessage">
<xsl:with-param name="reqLang" select="'default'"/>
</xsl:call-template>
</xsl:variable>
<xsl:sequence select="$assertMsgDef"/>
<!-- Print distinct diagnostics messages. -->
<xsl:variable name="allMessages" as="item()*">
<xsl:for-each select="$localizationNodes">
<xsl:call-template name="getMessage">
<xsl:with-param name="reqLang" select="'default'"/>
<xsl:with-param name="isAddId" select="true()"/>
</xsl:call-template>
</xsl:for-each>
</xsl:variable>
<xsl:sequence select="$allMessages"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<!-- Generate the assertion message, if no language match-->
<xsl:variable name="assertMsgDef">
<xsl:call-template name="getMessage">
<xsl:with-param name="reqLang" select="'default'"/>
</xsl:call-template>
</xsl:variable>
<xsl:sequence select="$assertMsgDef"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!-- Function used to obtain the message from the current node -->
<xsl:template name="getMessage">
<xsl:param name="reqLang" as="xs:string" required="yes"/>
<xsl:param name="isAddId" as="xs:boolean" select="false()"/>

<xsl:choose>
<xsl:when test="$reqLang != 'default'">
<!-- Get the language from the current node -->
<xsl:variable name="lang">
<xsl:variable name="currentLang" select="(ancestor-or-self::*/@xml:lang)[last()]"/>
<xsl:value-of select="if ($currentLang) then ($currentLang) else ('#NONE')"/>
</xsl:variable>
<!-- Generate the message from assert only if matches the current language -->
<xsl:if test="starts-with($lang, $reqLang)">
<xsl:if test="$isAddId">[#<xsl:value-of select="@id"/>]</xsl:if>
<xsl:apply-templates mode="text"/>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<!-- Generate the message with the language in front. -->
<xsl:if test="$isAddId">[#<xsl:value-of select="@id"/>]</xsl:if>
<xsl:if test="@xml:lang">[<xsl:value-of select="@xml:lang"/>]</xsl:if>
<xsl:apply-templates mode="text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>
4 changes: 3 additions & 1 deletion trunk/schematron/code/iso_schematron_skeleton_for_saxon.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,7 @@ which require a preprocess.
<xsl:call-template name="process-message">
<xsl:with-param name="pattern" select="$test"/>
<xsl:with-param name="role" select="$role"/>
<xsl:with-param name="diagnostics" select="$diagnostics"/>
</xsl:call-template>

<xsl:if test=" $terminate = 'yes' or $terminate = 'true' ">
Expand Down Expand Up @@ -2149,7 +2150,8 @@ which require a preprocess.
<!-- default output action: the simplest customization is to just override this -->
<xsl:template name="process-message">
<xsl:param name="pattern" />
<xsl:param name="role" />
<xsl:param name="role" />
<xsl:param name="diagnostics"/>

<xsl:apply-templates mode="text"/>
<xsl:if test=" $message-newline = 'true'" >
Expand Down
19 changes: 12 additions & 7 deletions trunk/schematron/code/iso_svrl_for_xslt2.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ THE SOFTWARE.
necessary for your system.
-->
<xsl:import href="iso_schematron_skeleton_for_saxon.xsl"/>

<!-- Support to generate message in multiple languages -->
<xsl:import href="iso_schematron_multilingual_message_xslt2.xsl"/>

<!--
<xsl:import href="iso_schematron_skeleton_for_xslt1.xsl"/>
Expand Down Expand Up @@ -302,8 +305,10 @@ THE SOFTWARE.
</xsl:if>

<svrl:text>
<xsl:apply-templates mode="text" />

<xsl:call-template name="generateTextMassage">
<xsl:with-param name="diagnostics" select="$diagnostics"/>
<xsl:with-param name="currentLanguage" select="$langCode"/>
</xsl:call-template>
</svrl:text>
<xsl:if test="$diagnose = 'yes' or $diagnose= 'true' ">
<!-- true/false is the new way -->
Expand Down Expand Up @@ -378,8 +383,10 @@ THE SOFTWARE.
</xsl:if>

<svrl:text>
<xsl:apply-templates mode="text" />

<xsl:call-template name="generateTextMassage">
<xsl:with-param name="diagnostics" select="$diagnostics"/>
<xsl:with-param name="currentLanguage" select="$langCode"/>
</xsl:call-template>
</svrl:text>
<xsl:if test="$diagnose = 'yes' or $diagnose='true' ">
<!-- true/false is the new way -->
Expand All @@ -405,9 +412,6 @@ THE SOFTWARE.
</xsl:if>
</xsl:template>




<xsl:template name="process-diagnostic">
<xsl:param name="id"/>
<!-- Rich parameters -->
Expand Down Expand Up @@ -581,6 +585,7 @@ THE SOFTWARE.
<xsl:template name="process-message" >
<xsl:param name="pattern"/>
<xsl:param name="role"/>
<xsl:param name="diagnostics"/>
</xsl:template>


Expand Down