xsl-list
[Top] [All Lists]

Re: [xsl] Help with transforming unique set of data in XSLT 2.0

2010-07-29 04:09:20
First, it's a good idea to declare the expected type of the parameter, and either say that it's required, or give a default value:

<xsl:param name="LIST_OF_FILE_PARAM" as="xs:string*" required="no" select="()"/>


Then iterating through the files is very straightforward:

<xsl:for-each select="document($LIST_OF_FILE_PARAM)">
   ....
</xsl:for-each>

Michael Kay
Saxonica



On 29/07/2010 09:40, Jack Bush wrote:
Hi Michael,

Thank you for identifying the issue which was caused by an iteration loop which
invoked the transformation twice. The purpose of this iteration was to pass each
secondary file found to the above stylesheet which has a side effect of looping
too many times. Instead, I would like to build an arraylist of secondary files
before passing over to the same stylesheet to carry out a single transformation
such as the following but not clear how XSLT could process it:

         Transformer transformer = transformerFactory.newTransformer(new
StreamSource(masterdox1StyleSheetBrIn));
         secondaryArrayList.add("file:///E:/fileA.xml");
         secondaryArrayList.add("file:///E:/fileB.xml");
         secondaryArrayList.add("file:///E:/fileC.xml");
         secondaryArrayList.add("file:///E:/fileD.xml");
         transformer.setParameter("LIST_OF_FILE_PARAM", secondaryArrayList);

The stylesheet would look something like:

<?xml version="1.0"?>
<!-- masterdox1.xsl -->
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

   <xsl:output method="html"/>

<xsl:param name="LIST_OF_FILE_PARAM" />

   <xsl:template match="/">
      <xsl:for-each select=".">
      </xsl:for-each>
   </xsl:template>

</xsl:stylesheet>

All I wanted for now is to iterate through each file so they could be passed to
document(). Saxon documentation gave the following explanation:

If the returned value is an instance of the Java class java.util.Collection, or
if it is an array, the XPath value will be the sequence represented by the
contents of this Collection or array. The members of the collection or array
will each be converted to an XPath value, as if each member was supplied from a
separate function call. An error is reported if the result contains a list or
array nested within another list or array. The contents of the list or array are
copied immediately on return from the function, so the original List or array
object itself may be safely re-used.


But I am not clear how this could be achieved on the XSLT side.

Thanks again,

Jack



----- Original Message ----
From: Michael Kay<mike(_at_)saxonica(_dot_)com>
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Sent: Tue, 27 July, 2010 6:05:33 PM
Subject: Re: [xsl] Help with transforming unique set of data in XSLT 2.0

On 27/07/2010 08:29, Jack Bush wrote:
Hi All,

I am having difficulty get the following stylesheet to transform unique set of
data once:


The fact that your output shows two XML declarations suggests that there
is something wrong with the way you are running the transformation or
displaying the results. There's no way the XSLT code you have shown will
produce two separate result documents, each with its own XML declaration.

Incidentally, the argument to document() should be a URI, not a Windows
filename. Many processors let you get away with this error, but there's
nothing in the language spec to encourage this and it won't work with
all processors.

Michael Kay
Saxonica

The following output generated duplicated set of identical transformed data
when
using<xsl:for-each>:

<?xml version="1.0" encoding="UTF-8"?>
<employee>
    <first_name>John</first_name>
    <surname>smith</surname>
    <sex>M</sex>
    <first_name>Kelly</first_name>
    <surname>Jones</surname>
    <sex>F</sex>
</employee>

<?xml version="1.0" encoding="UTF-8"?>
<employee>
    <first_name>John</first_name>
    <surname>smith</surname>
    <sex>M</sex>
    <first_name>Kelly</first_name>
    <surname>Jones</surname>
    <sex>F</sex>
</employee>

I tried the following changes after suspecting that the cause was from the
<xsl:for-each>   statement:

1<xsl:template match="/">
2<xsl:apply-templates select="document('E:/employee.xml')/ns:html"/>
3</xsl:template>
4
5<xsl:template match="ns:html">
6<employee>
7<xsl:variable name="employee_data"
select="ns:body/ns:div[(_at_)id='content']/ns:table[(_at_)class='sresults']/ns:tr/ns:td/ns:a"/>
8<xsl:apply-templates select="$employee_data"/>
9<first_name><xsl:value-of
select="substring-before(@title,',')"/></first_name>
10<surname><xsl:value-of select="@href"/></surname>
11<sex><xsl:value-of select="@gender"/></sex>
12</employee>
13</xsl:template>

Below is the output without using<xsl:for-each>:

<?xml version="1.0" encoding="UTF-8"?>
<employee>
    John Kelly
    <firstname />
    <surname />
    <sex/>>
</employee>

<?xml version="1.0" encoding="UTF-8"?>
<employee>
    John Kelly
    <firstname />
    <surname />
    <sex/>>
</employee>

Any ideas on what is causing the duplications and why is the second stylesheet
picking up the firstnames of 2 separate xml documents in the wrong place?

The desire output is just a single entry of each record such as the following:

<?xml version="1.0" encoding="UTF-8"?>
<employee>
    <first_name>John</first_name>
    <surname>smith</surname>
    <sex>M</sex>
    <first_name>Kelly</first_name>
    <surname>Jones</surname>
    <sex>F</sex>
</employee>

Thanks a lot,

Jack





--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail:<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail:<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--





--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail:<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>