xsl-list
[Top] [All Lists]

Re: [xsl] what's the best way to validate input parameters to a stylesheet?

2021-03-19 17:08:12
Hey Chris,

You could pull the logic into a static variable:

<xsl:variable name="doc-fails" as="xs:boolean" select="matches($from.att,
'^@\w+=\w+$') and matches($to.att, '^@\w+=\w+$')"/>

then

<xsl:template match="/[$doc-fails]">
  ... this document fails ...
</xsl:template>

Make the @priority explicit if you want to give a better hint.

It's really factoring out the choose into a template match in the normal
way. You could even do it without the variable for maximum mystery.

Cheers, Wendell



On Fri, Mar 19, 2021 at 5:58 PM Chris Papademetrious
christopher(_dot_)papademetrious(_at_)synopsys(_dot_)com <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Hello! I’ve got a stylesheet that takes two string parameters:



<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">



  <xsl:param name="from.att" as="xs:string"/>

  <xsl:param name="to.att"   as="xs:string"/>



Both parameters are expected to be strings in the form of



@attributename=value



At first I tried adding a check at the top level of the stylesheet:



<xsl:if test="not(matches($from.att, '^@\w+=\w+$') and matches($to.att,
'^@\w+=\w+$'))">

  <xsl:message terminate="yes">Input parameters must be in the form
'@attributename=value'.</xsl:message>

</xsl:if>



but <xsl:if> isn’t allowed at the top level of a stylesheet.



So for now I’m doing this in a “dummy” root element template that doesn’t
do anything:



  <!-- this doesn't actually do anything other than validate our input
parameters -->

  <xsl:template match="/" priority="20">

    <xsl:choose>

      <xsl:when test="matches($from.att, '^@\w+=\w+$') and
matches($to.att, '^@\w+=\w+$')">

        <xsl:next-match/>

      </xsl:when>

      <xsl:otherwise>

        <xsl:message>Input parameters must be in the form
'@attributename=value'.</xsl:message>

        <xsl:copy-of select="."/>

      </xsl:otherwise>

   </xsl:choose>

  </xsl:template>



but I feel like there’s probably a better way. Is there?



-----
Chris Papademetrious

Tech Writer, Implementation Group

(610) 628-9718 home office

(570) 460-6078 cell


XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/174322> (by
email <>)



-- 
...Wendell Piez... ...wendell -at- nist -dot- gov...
...wendellpiez.com... ...pellucidliterature.org... ...pausepress.org...
...github.com/wendellpiez... ...gitlab.coko.foundation/wendell...
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>