You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by "Schwartz, Rechell R, NLCIO" <rr...@att.com> on 2001/06/14 16:48:31 UTC
Parameter Sorting Problem With Xalan
Hello,
I have been banging my head against the wall trying to sort with a a
parameter-based xsl stylesheet. I have tried the suggestions posted in
previous e-mails on this subject as well as in articles on the Web, but I
still can't get the sorting to work. Basically, I am trying to take an xml
file and convert it to another xml file that is sorted based on a column and
sort order that are dynamically selected by the user. I pass the parameters
for the column name and sort order in as strings to the stylesheet using the
Tarsnformer's setParameter() method. The problem is that the stylesheet
always uses the default values for the column names and the sort order,
regardless of what I pass in as a parameter. Any help would be GREATLY
appreciated.
Rechell Schwartz
Here is the code for how I set the parameters:
public void sortXSL(String sortParameter, String sortOrder) throws
TransformerException, TransformerConfigurationException,
FileNotFoundException
{
TransformerFactory tFactory = TransformerFactory.newInstance();
String xsl = "c:\\directoryname\\xmlStyleSheet.xsl";
Transformer transformer = tFactory.newTransformer(new
StreamSource(xsl));
DOMResult domResult = new DOMResult();
transformer.setParameter("sortcolumn",
sortParameter.trim().toUpperCase());
transformer.setParameter("sortorder", sortOrder);
transformer.transform(new DOMSource(document), domResult);
document = (Document)domResult.getNode();
}
Here is the xsl stylesheet:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl=" http://www.w3.org/1999/XSL/Transform
<http://www.w3.org/1999/XSL/Transform> " version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="xml">
<xml>
<data>
<xsl:apply-templates/>
</data>
</xml>
</xsl:template>
<xsl:template match="data">
<xsl:param name="sortcolumn" select="'COLUMN1'"/>
<xsl:param name="sortorder" select="'ascending'"/>
<xsl:apply-templates select="row">
<xsl:sort order="{$sortorder}" data-type="text"
select="*[name()=$sortcolumn]"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="row">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="columntype">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="truncated">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="truncationsize">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
Finally, here is the xml file that is inputted to the stylesheet:
<xml>
<data>
<row>
<COLUMN1>ABC</COLUMN1><COLUMN2>ABC2</COLUMN2>
</row>
<row>
<COLUMN1>GHI</COLUMN1><COLUMN2>GHI2</COLUMN2>
</row>
<row>
<COLUMN1>DEF</COLUMN1><COLUMN2>DEF2</COLUMN2>
</row>
<columntype><COLUMN1>string</COLUMN1><COLUMN2>string</COLUMN2>
<truncated>no</truncated>
<truncationsize>1000</truncationsize>
</data>
</xml>
Re: Parameter Sorting Problem With Xalan
Posted by Gary L Peskin <ga...@firstech.com>.
Rechell -
Sorry to take so long in responding. It's been a hectic morning.
You are passing the parameters to the top level of the stylesheet but
you are trying to receive them as parameters to the template with
match="data". Unfortunately, they were not passed to the that template
by the "xml" template so there is nothing to receive and you get the
default values.
If you move your two xsl:param elements to just after the xsl:output
element, everything should work as you expect since global parameters
are known in all templates. You could actually move them out of the
xsl:template and put them in between any of the templates so that they'd
be at the top level but I like to keep them at the top so that the
top-level parameters are easily spotted.
HTH,
Gary
> "Schwartz, Rechell R, NLCIO" wrote:
>
> Hello,
>
> I have been banging my head against the wall trying to sort with a a
> parameter-based xsl stylesheet. I have tried the suggestions posted in
> previous e-mails on this subject as well as in articles on the Web,
> but I still can't get the sorting to work. Basically, I am trying to
> take an xml file and convert it to another xml file that is sorted
> based on a column and sort order that are dynamically selected by the
> user. I pass the parameters for the column name and sort order in as
> strings to the stylesheet using the Tarsnformer's setParameter()
> method. The problem is that the stylesheet always uses the default
> values for the column names and the sort order, regardless of what I
> pass in as a parameter. Any help would be GREATLY appreciated.
>
> Rechell Schwartz
>
> Here is the code for how I set the parameters:
> public void sortXSL(String sortParameter, String sortOrder) throws
> TransformerException, TransformerConfigurationException,
> FileNotFoundException
> {
> TransformerFactory tFactory = TransformerFactory.newInstance();
> String xsl = "c:\\directoryname\\xmlStyleSheet.xsl";
> Transformer transformer = tFactory.newTransformer(new
> StreamSource(xsl));
> DOMResult domResult = new DOMResult();
> transformer.setParameter("sortcolumn",
> sortParameter.trim().toUpperCase());
> transformer.setParameter("sortorder", sortOrder);
> transformer.transform(new DOMSource(document), domResult);
> document = (Document)domResult.getNode();
>
> }
>
> Here is the xsl stylesheet:
> <?xml version="1.0"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> version="1.0">
> <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
>
> <xsl:template match="xml">
> <xml>
> <data>
> <xsl:apply-templates/>
> </data>
> </xml>
> </xsl:template>
>
> <xsl:template match="data">
> <xsl:param name="sortcolumn" select="'COLUMN1'"/>
> <xsl:param name="sortorder" select="'ascending'"/>
> <xsl:apply-templates select="row">
> <xsl:sort order="{$sortorder}" data-type="text"
> select="*[name()=$sortcolumn]"/>
> </xsl:apply-templates>
> </xsl:template>
>
> <xsl:template match="row">
> <xsl:copy-of select="."/>
> </xsl:template>
>
> <xsl:template match="columntype">
> <xsl:copy-of select="."/>
> </xsl:template>
>
> <xsl:template match="truncated">
> <xsl:copy-of select="."/>
> </xsl:template>
>
> <xsl:template match="truncationsize">
> <xsl:copy-of select="."/>
> </xsl:template>
>
> </xsl:stylesheet>
>
> Finally, here is the xml file that is inputted to the stylesheet:
>
> <xml>
> <data>
> <row>
> <COLUMN1>ABC</COLUMN1><COLUMN2>ABC2</COLUMN2>
> </row>
> <row>
> <COLUMN1>GHI</COLUMN1><COLUMN2>GHI2</COLUMN2>
> </row>
> <row>
> <COLUMN1>DEF</COLUMN1><COLUMN2>DEF2</COLUMN2>
> </row>
> <columntype><COLUMN1>string</COLUMN1><COLUMN2>string</COLUMN2>
> <truncated>no</truncated>
> <truncationsize>1000</truncationsize>
> </data>
> </xml>
Re: Parameter Sorting Problem With Xalan
Posted by Pavlin Djokov <dj...@dir.bg>.
<xsl:sort order="{$sortorder}" data-type="text" select="*[name()=$sortcolumn]"/> should be
<xsl:sort data-type="text" select="*[name()=$sortcolumn]">
<xsl:attribute name="order"><xsl:value-of select="$sortorder"/></xsl:attribute>
</xsl:sort>
----- Original Message -----
From: Schwartz, Rechell R, NLCIO
To: 'xalan-dev@xml.apache.org'
Sent: Thursday, June 14, 2001 5:48 PM
Subject: Parameter Sorting Problem With Xalan
Hello,
I have been banging my head against the wall trying to sort with a a parameter-based xsl stylesheet. I have tried the suggestions posted in previous e-mails on this subject as well as in articles on the Web, but I still can't get the sorting to work. Basically, I am trying to take an xml file and convert it to another xml file that is sorted based on a column and sort order that are dynamically selected by the user. I pass the parameters for the column name and sort order in as strings to the stylesheet using the Tarsnformer's setParameter() method. The problem is that the stylesheet always uses the default values for the column names and the sort order, regardless of what I pass in as a parameter. Any help would be GREATLY appreciated.
Rechell Schwartz
Here is the code for how I set the parameters:
public void sortXSL(String sortParameter, String sortOrder) throws TransformerException, TransformerConfigurationException, FileNotFoundException
{
TransformerFactory tFactory = TransformerFactory.newInstance();
String xsl = "c:\\directoryname\\xmlStyleSheet.xsl";
Transformer transformer = tFactory.newTransformer(new StreamSource(xsl));
DOMResult domResult = new DOMResult();
transformer.setParameter("sortcolumn", sortParameter.trim().toUpperCase());
transformer.setParameter("sortorder", sortOrder);
transformer.transform(new DOMSource(document), domResult);
document = (Document)domResult.getNode();
}
Here is the xsl stylesheet:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="xml">
<xml>
<data>
<xsl:apply-templates/>
</data>
</xml>
</xsl:template>
<xsl:template match="data">
<xsl:param name="sortcolumn" select="'COLUMN1'"/>
<xsl:param name="sortorder" select="'ascending'"/>
<xsl:apply-templates select="row">
<xsl:sort order="{$sortorder}" data-type="text" select="*[name()=$sortcolumn]"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="row">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="columntype">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="truncated">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="truncationsize">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
Finally, here is the xml file that is inputted to the stylesheet:
<xml>
<data>
<row>
<COLUMN1>ABC</COLUMN1><COLUMN2>ABC2</COLUMN2>
</row>
<row>
<COLUMN1>GHI</COLUMN1><COLUMN2>GHI2</COLUMN2>
</row>
<row>
<COLUMN1>DEF</COLUMN1><COLUMN2>DEF2</COLUMN2>
</row>
<columntype><COLUMN1>string</COLUMN1><COLUMN2>string</COLUMN2>
<truncated>no</truncated>
<truncationsize>1000</truncationsize>
</data>
</xml>