xsl-list
[Top] [All Lists]

Re: [xsl] user defined data elements in stylesheets

2019-12-06 05:05:56
Am 06.12.2019 um 11:55 schrieb Mukul Gandhi 
gandhi(_dot_)mukul(_at_)gmail(_dot_)com:
Hi all,
   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>
   <a val="-1"/>
   <a val="-4"/>
   <a val="5"/>
   <a val="3"/>
   <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";
                         xmlns:m_ns0="http://example.com/my_meta_data";
                         exclude-result-prefixes="m_ns0"
                         version="3.0">

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

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

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

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

</xsl:stylesheet>

The above transformation produces, following result,

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

   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.

--~----------------------------------------------------------------
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>