Home > Net >  Skip a template in XSLT run command
Skip a template in XSLT run command

Time:07-11

I'm trying to validate my XML using XSL and producing the log as an HTML report. The XSL has three templates with name "affd", "affc" and "copy". I'm executing this code using the run command

java -jar saxon8.9.0.jar -o aaam-003-vtout.html AAM-01-2019-0003.xml identity.xsl -l

However, I want to skip or ignore a template using the run command. For example, if I don't want to run "affd" template, how can I skip this in XSL command line.

My xsl code

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"    xmlns:exsl="http://exslt.org/common"    xmlns:xlink="http://www.w3.org/1999/xlink"      xmlns:mml="http://www.w3.org/1998/Math/MathML"  xmlns:saxon="http://saxon.sf.net/"  xmlns:file="java.io.File"   xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="exsl file" extension-element-prefixes="saxon"  version='2.0'>
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:variable name="aff-count" select="1" saxon:assignable="yes"/>
    <xsl:variable name="copyright-count" select="1" saxon:assignable="yes"/>
    <xsl:template match="/">
        <html>
            <title>Log Report</title>
            <style>table {  border-collapse: collapse;  }   table, th, td { border: 1px solid black;    }   thead {background: #bed3fa}</style>
            <body>
                <h1 align="center">Log Report</h1>
                <hr/>
                <br/>
                <table align="center" border="none" width="100%">
                    <thead>
                        <tr style="font-size:14pt; font-weight:bold;" border="solid thin;">
                            <th>Check ID</th>
                            <th>Element Name</th>
                            <th>Error Description</th>
                            <th>Error Position</th>
                        </tr>
                    </thead>
                    <tbody>
                        <xsl:call-template name="affd"/>
                        <xsl:call-template name="affc"/>
                        <xsl:call-template name="copy"/>
                    </tbody>
                </table>
            </body>
        </html>
    </xsl:template>
    <!--Affiliation Department check-->
    <xsl:template name="affd">
        <xsl:for-each select="descendant::*">
            <xsl:choose>
                <xsl:when test="matches(local-name(),'aff')">
                    <xsl:if test="not(matches(.,'(xxx)'))">
                        <tr>
                            <td>AFF<xsl:value-of select="$aff-count"/>
                            </td>
                            <saxon:assign name="aff-count" select="$aff-count 1"/>
                            <td>
                                <xsl:value-of select="name()"/>
                            </td>
                            <td>
                                <xsl:text>"D" missing</xsl:text>
                            </td>
                            <td>
                                <xsl:value-of select="@error-position"/><xsl:value-of select="saxon:line-number()"/>:<xsl:value-of select="saxon:column-number()"/>
                            </td>
                        </tr>
                    </xsl:if>
                </xsl:when>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>
    <!--Affiliation city and country check-->
    <xsl:template name="affc">
        <xsl:for-each select="descendant::*">
            <xsl:choose>
                <xsl:when test="matches(local-name(),'aff')">
                    <xsl:if test="not(descendant::*:c)">
                        <tr>
                            <td>AFF2<xsl:value-of select="$aff-count"/>
                            </td>
                            <saxon:assign name="aff-count" select="$aff-count 1"/>
                            <td>c</td>
                            <td>C missing</td>
                            <td>
                                <xsl:value-of select="@error-position"/>
                            </td>
                        </tr>
                    </xsl:if>
                </xsl:when>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>
    <!--Copyright symbol in Copyright tag-->
    <xsl:template name="copy">
        <xsl:for-each select="descendant::*">
            <xsl:choose>
            <xsl:when test="matches(local-name(),'copy')">
            <xsl:if test="not(matches(.,'(&#x00A9;)'))">
                  <tr>
                    <td>CPY<xsl:value-of select="$copy"/></td>
                    <saxon:assign name="copy" select="$copy 1"/>
                    <td><xsl:value-of select="name()"/></td>
                    <td><xsl:text>Copy missing</xsl:text></td>
                    <td><xsl:value-of select="@error-position"/></td>
                  </tr>
              </xsl:if>
              </xsl:when>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>```

CodePudding user response:

This code is written in a very unidiomatic way. Normal XSLT style would be to replace (for example) the template

<xsl:template name="affc">
        <xsl:for-each select="descendant::*">
            <xsl:choose>
                <xsl:when test="matches(local-name(),'aff')">
                    <xsl:if test="not(descendant::*:c)">
                        <tr>
                            <td>AFF2<xsl:value-of select="$aff-count"/>
                            </td>
                            <saxon:assign name="aff-count" select="$aff-count 1"/>
                            <td>c</td>
                            <td>C missing</td>
                            <td>
                                <xsl:value-of select="@error-position"/>
                            </td>
                        </tr>
                    </xsl:if>
                </xsl:when>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>

with

<xsl:template match="*:aff[empty(../*:c)]" mode="affc">
   <tr>
      <td>AFF2<xsl:value-of select="$aff-count"/></td>
      <saxon:assign name="aff-count" select="$aff-count 1"/>
      <td>c</td>
      <td>C missing</td>
      <td><xsl:value-of select="@error-position"/></td>
   </tr>
</xsl:template>

and to replace the relevant xsl:call-template with <xsl:apply-templates select=".//*" mode="affc"/>.

I would also get rid of the saxon:assign but this requires a little more restructuring of your code.

To parameterise which checks you apply, add an xsl:param to your stylesheet, for example

<xsl:param name="checks" select="'ABC'"/>

then put a conditional around the call-template (or apply-templates):

<xsl:if test="contains($checks, 'A')">
  <xsl:call-template ..../>
</xsl:if>

and on the command line specify the checks you want with (for example) checks=AC'

CodePudding user response:

Before the <xsl:variable>s, insert a parameter

<xsl:param name="omit_affd"/>

and omit calling the template if that is set:

<xsl:if test="not($omit_affd)">
  <xsl:call-template name="affd"/>
</xsl:if>

You set the parameter on the command line:

java -jar saxon8.9.0.jar -o a.html a.xml a.xsl omit_affd=yes
  • Related