Re: [xsl] user defined data elements in stylesheets

Subject: Re: [xsl] user defined data elements in stylesheets
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 6 Dec 2019 11:05:49 -0000
Am 06.12.2019 um 11:55 schrieb Mukul Gandhi gandhi.mukul@xxxxxxxxx:
Hi all,
B  B I'm trying to use 'user defined data elements' in an XSLT stylesheet.

Following is my XSLT transformation example,

XML input document:

<?xml version="1.0" encoding="UTF-8"?>
<root>
B  B <a val="-1"/>
B  B <a val="-4"/>
B  B <a val="5"/>
B  B <a val="3"/>
B  B <a val="2"/>
</root>

XSLT stylesheet (file proc1.xsl, with local absolute URI
file:///e:/xslt_examples/basic/proc1.xsl):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
B B B B B B B B B B B B
B xmlns:m_ns0="http://example.com/my_meta_data";
B  B  B  B  B  B  B  B  B  B  B  B  B exclude-result-prefixes="m_ns0"
B  B  B  B  B  B  B  B  B  B  B  B  B version="3.0">

B  B  <xsl:output method="xml" indent="yes"/>
B  B  <!-- this is the user defined data element -->
B  B  <m_ns0:names>
B  B  B  B <name>jill</name>
B  B  B  B <name>jane</name>
B  B  B  B <name>hello1</name>
B  B  </m_ns0:names>

B  B  <xsl:template match="root">
B  B  B  B <result>
B  B  B  B  B  <xsl:copy-of

select="document('file:///e:/xslt_examples/basic/proc1.xsl')/xsl:stylesheet/m
_ns0:names"
copy-namespaces="no"/>
B  B  B  B  B  <xsl:apply-templates select="a[number(@val) gt 0]"
mode="gt0"/>
B B B B B <xsl:apply-templates select="a[number(@val) lt 0]"
mode="lt0"/>
B  B  B  B </result>
B  B  </xsl:template>

B  B  <xsl:template match="a" mode="gt0">
B  B  B  <val><xsl:value-of select="@val"/>: positive</val>
B  B  </xsl:template>

B  B  <xsl:template match="a" mode="lt0">
B  B  B  <val><xsl:value-of select="@val"/>: negative</val>
B  B  </xsl:template>

</xsl:stylesheet>

The above transformation produces, following result,

<?xml version="1.0" encoding="UTF-8"?>
<result>
B  B <m_ns0:names xmlns:m_ns0="http://example.com/my_meta_data";>
B  B  B  <name>jill</name>
B  B  B  <name>jane</name>
B  B  B  <name>hello1</name>
B  B </m_ns0:names>
B  B <val>5: positive</val>
B  B <val>3: positive</val>
B  B <val>2: positive</val>
B  B <val>-1: negative</val>
B  B <val>-4: negative</val>
</result>

The above output is what I'm expecting.

Following are my questions,

You could see that, within my stylesheet, I've used following
stylesheet instruction,
<xsl:copy-of

select="document('file:///e:/xslt_examples/basic/proc1.xsl')/xsl:stylesheet/m
_ns0:names"
copy-namespaces="no"/>

I've used the absolute URI of the stylesheet document, as argument to
the document() function. Is it possible, to specify the stylesheet
document reference to the document() function in a portable way (i.e,
not an absolute URI)?


The usual way is


B B document('')

but it is not portable in any case, if the stylesheet was loaded from a
string for instance, that call might fail.

In XSLT 2 and 3 I think I would prefer to use a parameter or variable
with element contents instead of user defined data elements, I think
user defined data elements are mainly useful in XSLT 1 to work around
the result tree restriction of any param/variable containing nodes.

Current Thread