XSL Parameters / XQuery Variables

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  User Guide and Reference > Menu Commands > XSL/XQuery Menu >

XSL Parameters / XQuery Variables

The XSL/XQuery | XSL Parameters/XQuery Variables command opens the XSLT Input Parameters/XQuery External Variables dialog (see screenshot). You can enter the name of one or more parameters you wish to pass to the XSLT stylesheet, or one or more external XQuery variables you wish to pass to the XQuery document, and their respective values. These parameters are used as follows in XMLSpy:

 

When the XSL Transformation command in the XSL/XQuery menu is used to transform an XML document, the parameter values currently saved in the dialog are passed to the selected XSLT document and used for the transformation.
When the XQuery Execution command in the XSL/XQuery menu is used to process an XQuery document, the XQuery external variable values currently saved in the dialog are passed to the XQuery document for the execution.

 

Note:Parameters or variables that you enter in the XSLT Input Parameters/XQuery External Variables dialog are only passed on to the built-in Altova XSLT engine. Therefore, if you are using MSXML or another external engine that you have configured, these parameters are not passed to this engine.

 

Note:It is not an error if an XSLT parameter or external XQuery variable is defined in the XSLT Input Parameters/XQuery External Variables dialog but is not used in the XSLT/XQuery document or the transformation.

 

Using XSLT Parameters

The value you enter for the parameter can be an XPath expression without quotes or a text string delimited by quotes. If the active document is an XSLT document, the Get from XSL button will be enabled. Clicking this button inserts parameters declared in the XSLT into the dialog together with their default values. This enables you to quickly include declared parameters and then change their default values as required.

xsl_parameters

Note:Once a set of parameter-values is entered in the dialog, it is used for all subsequent transformations until it is explicitly deleted or the application is restarted. Parameters entered in the dialog are specified at the application-level for that session, and will be passed to the respective XSLT document for every transformation that is carried out via the IDE from that moment onward. This means that:

 

parameters are not associated with any particular document
any parameter entered in the dialog is erased once XMLSpy has been closed.

 

Usage example for XSLT parameters

We have an XML document that contains the names of countries and their respective capitals:

 

<document>

   <countries>

      <country name="USA" capital="Washington DC"/>

      <country name="UK" capital="London"/>

      <country name="France" capital="Paris"/>

      <country name="Russia" capital="Moscow"/>

      <country name="China" capital="Beijing"/>

   </countries>

</document>

 

The following XSLT document will generate an XML document that displays one country from the XML file together with that country's capital. The country is selected by entering its name as the value of the parameter named country (shown highlighted in yellow below).

 

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

   <xsl:param name="country" select="'USA'"/>

   <xsl:template match="countries">

      <xsl:for-each select="country[@name=$country]">

         <country>

            <name><xsl:value-of select="$country"/></name>

            <capital><xsl:value-of select="@capital"/></capital>

         </country>

      </xsl:for-each>

   </xsl:template>

</xsl:stylesheet>

 

When this XSLT document is run on the XML document listed above, the result will be this:

 

<country><name>USA</name><capital>Washington DC</capital></country>

 

Now, if in the XSLT Input Parameters/XQuery External Variables dialog you create a parameter named country and give it a value (see screenshot above), then this value will be passed to the parameter country in the XSLT stylesheet for the transformation. In this way, you can pass different values to different parameters at run time.

 

 

Note:

 

If you use the XSL:FO Transformation command (XSL/XQuery | XSL:FO Transformation), then parameters entered in the XSLT Input Parameters/XQuery External Variables dialog are not passed to the stylesheet. In order for these parameters to be used in PDF output, first transform from XML to FO using the XSLT Transformation command (XSL/XQuery | XSL Transformation), and then transform the FO to PDF using the XSL:FO Transformation command (XSL/XQuery | XSL:FO Transformation).
If you use an XSLT processor other than the built-in Altova XSLT Engines, parameters you enter using the Input Parameters dialog will not be passed to the external processor.

 

Using external XQuery variables

The value you enter for an external XQuery variable could be an XPath expression without quotes or a text string delimited by quotes. The datatype of the external variable is specified in the variable declaration in the XQuery document.

xquery_variables

Note:Once a set of external XQuery variables are entered in the dialog, they are used for all subsequent executions until they are explicitly deleted or the application is restarted. Variables entered in the dialog are specified at the application-level, and will be passed to the respective XQuery document for every execution that is carried out via the IDE from that moment onward. This means that:

 

Variables are not associated with any particular document
Any variable entered in the dialog is erased once the application (XMLSpy) has been closed down.

 

Usage example for external XQuery variables

In the following example, a variable $first is declared in the XQuery document and is then used in the return clause of the FLWOR expression:

 

 xquery version "1.0";

 declare variable $first as xs:string external;

 let $last := "Jones"

 return concat($first, " ", $last )

 

This XQuery returns Peter Jones, if the value of the external variable (entered in the XSLT Input Parameters/XQuery External Variables dialog) is Peter. Note the following:

 

The external keyword in the variable declaration in the XQuery document indicates that this variable is an external variable.
Defining the static type of the variable is optional. If a datatype for the variable is not specified in the variable declaration, then the variable value is assigned the type xs:untypedAtomic.
If an external variable is declared in the XQuery document, but no external variable of that name is passed to the XQuery document, then an error is reported.
If an external variable is declared and is entered in the XSLT Input Parameters/XQuery External Variables dialog, then it is considered to be in scope for the XQuery document being executed. If a new variable with that name is declared within the XQuery document, the new variable temporarily overrides the in-scope external variable. For example, the XQuery document below returns Paul Jones even though the in-scope external variable $first has a value of Peter.

 

xquery version "1.0";

declare variable $first as xs:string external;

let $first := "Paul"

let $last := "Jones"

return concat($first, " ", $last )

 


© 2019 Altova GmbH