Variable definition not processed when variable isn't output

Here should go questions about transforming XML with XSLT and FOP.
mboudreau
Posts: 20

Variable definition not processed when variable isn't output

Thu Sep 28, 2017 12:25 am

I was setting up a stylesheet in which I expected to use a number of variables. Two of my variable definitions were as follows:

Code: Select all

<xsl:variable name="doi">
    <xsl:choose>
        <xsl:when test="/article/front/article-meta/article-id[@pub-id-type = 'doi']">
            <xsl:value-of select="/article/front/article-meta/article-id[@pub-id-type = 'doi']"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:message>ERROR: Cannot find DOI</xsl:message>
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

<xsl:variable name="doi-suffix">
    <xsl:value-of select="replace($doi, '^10.[0-9]+/', '')"/>
</xsl:variable>


When I ran the transformation on a file that I knew was missing the expected <article-id> element, I was surprised not to see the error message appear. Eventually I discovered that when neither the 'doi' nor the 'doi-suffix' variable was used in any of my templates, the variable definitions seemed not to be processed at all. When I added either variable to the output of any template, things worked as expected again.

I don't know if this is a characteristic of Saxon (my transformation is using the SaxonPE-9.6.0.7 processor) or Oxygen (version 18.1), but it feels buggy.
Radu
Posts: 5088

Re: Variable definition not processed when variable isn't output

Thu Sep 28, 2017 9:53 am

Hi,

I'm not sure what you mean by this:

but it feels buggy


You defined two variables but you did not use them anywhere. So of course they produced no effect on the output. That's how the XSLT standard works.
Also if you have an xsl:value pointing to some non existing XPath:

Code: Select all

<xsl:value-of select="/nonExistingElement"/>


the processing will not break. The value returned in this case will be empty. Again, that's how the XSLT standard works and the Saxon processor which is used by Oxygen to transform XML documents using XSLT implements the standard.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Return to “XSLT and FOP”

Who is online

Users browsing this forum: No registered users and 1 guest