You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Marcelo Iturbe <ma...@santiago.cl> on 2011/03/30 19:24:07 UTC

Question regarding XSLT

Hello,
I currently have set up Solr working and I am doing tests with the XSL
stylesheets.

I had no problem in generating HTML files, but while trying to generate Json
files I noticed something odd..

I am calling Solr with the following URL:
http://172.16.0.30:8983/solr/gcontacts/select?q=apache*~&fl=all_text&wt=xslt&tr=example.xsl

What I noticed is, that no matter what you place in the XSLT file, you
ALWAYS get
<?xml version="1.0" encoding="UTF-8"?>
as the first line of the document. Is there a way to avoid this?

This is the XSL that I am using:

<xsl:stylesheet version='1.0'
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
>
  <xsl:output media-type="text/json; charset=UTF-8" />

  <xsl:template match='/'>
      searchAsYouType.handleAjaxResponse({"query":"term",
"results":[<xsl:apply-templates select="response/result/doc"/>]})
  </xsl:template>

  <xsl:template match="doc">
    <xsl:variable name="pos" select="position()"/>
        <xsl:apply-templates>
          <xsl:with-param name="pos"><xsl:value-of
select="$pos"/></xsl:with-param>
        </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="doc/arr" priority="100">
        <xsl:for-each select="*">
            {"name":"<xsl:value-of
select="."/>","type":"text/html","content":"","moreDetailsUrl":"
http://somedomain.com/","style":"normal"},
        </xsl:for-each>
  </xsl:template>

  <xsl:template match="*"/>

</xsl:stylesheet>

This produces the following text:
<?xml version="1.0" encoding="UTF-8"?>
      searchAsYouType.handleAjaxResponse({"query":"term", "results":[
            {"name":"Apache
Server","type":"text/html","content":"","moreDetailsUrl":"
http://somedomain.comn/","style":"normal"},
 ...

Since the first line is <?xml version="1.0" encoding="UTF-8"?>, it is
killing my script.

did I miss something?

Thanks for the help.

Re: Question regarding XSLT

Posted by Marcelo Iturbe <ma...@santiago.cl>.
That did the trick! thanks!

On Wed, Mar 30, 2011 at 1:31 PM, Steven A Rowe <sa...@syr.edu> wrote:

> Hi Marcelo,
>
> Try adding the 'method="text"' attribute to your <xsl:output> tag, e.g.:
>
> <xsl:output media-type="text/json; charset=UTF-8" method="text" />
>
> If that doesn't work, there is another attribute "omit-xml-declaration"
> that might do the trick.
>
> See http://www.w3.org/TR/xslt#output for more info.
>
> Steve
>
> > -----Original Message-----
> > From: Marcelo Iturbe [mailto:marcelo@santiago.cl]
> > Sent: Wednesday, March 30, 2011 1:24 PM
> > To: solr-user@lucene.apache.org
> > Subject: Question regarding XSLT
> >
> > Hello,
> > I currently have set up Solr working and I am doing tests with the XSL
> > stylesheets.
> >
> > I had no problem in generating HTML files, but while trying to generate
> > Json
> > files I noticed something odd..
> >
> > I am calling Solr with the following URL:
> >
> http://172.16.0.30:8983/solr/gcontacts/select?q=apache*~&fl=all_text&wt=xs
> > lt&tr=example.xsl
> >
> > What I noticed is, that no matter what you place in the XSLT file, you
> > ALWAYS get
> > <?xml version="1.0" encoding="UTF-8"?>
> > as the first line of the document. Is there a way to avoid this?
> >
> > This is the XSL that I am using:
> >
> > <xsl:stylesheet version='1.0'
> >     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
> > >
> >   <xsl:output media-type="text/json; charset=UTF-8" />
> >
> >   <xsl:template match='/'>
> >       searchAsYouType.handleAjaxResponse({"query":"term",
> > "results":[<xsl:apply-templates select="response/result/doc"/>]})
> >   </xsl:template>
> >
> >   <xsl:template match="doc">
> >     <xsl:variable name="pos" select="position()"/>
> >         <xsl:apply-templates>
> >           <xsl:with-param name="pos"><xsl:value-of
> > select="$pos"/></xsl:with-param>
> >         </xsl:apply-templates>
> >   </xsl:template>
> >
> >   <xsl:template match="doc/arr" priority="100">
> >         <xsl:for-each select="*">
> >             {"name":"<xsl:value-of
> > select="."/>","type":"text/html","content":"","moreDetailsUrl":"
> > http://somedomain.com/","style":"normal"},
> >         </xsl:for-each>
> >   </xsl:template>
> >
> >   <xsl:template match="*"/>
> >
> > </xsl:stylesheet>
> >
> > This produces the following text:
> > <?xml version="1.0" encoding="UTF-8"?>
> >       searchAsYouType.handleAjaxResponse({"query":"term", "results":[
> >             {"name":"Apache
> > Server","type":"text/html","content":"","moreDetailsUrl":"
> > http://somedomain.comn/","style":"normal"},
> >  ...
> >
> > Since the first line is <?xml version="1.0" encoding="UTF-8"?>, it is
> > killing my script.
> >
> > did I miss something?
> >
> > Thanks for the help.
>

RE: Question regarding XSLT

Posted by Steven A Rowe <sa...@syr.edu>.
Hi Marcelo,

Try adding the 'method="text"' attribute to your <xsl:output> tag, e.g.:

<xsl:output media-type="text/json; charset=UTF-8" method="text" />

If that doesn't work, there is another attribute "omit-xml-declaration" that might do the trick.

See http://www.w3.org/TR/xslt#output for more info.

Steve

> -----Original Message-----
> From: Marcelo Iturbe [mailto:marcelo@santiago.cl]
> Sent: Wednesday, March 30, 2011 1:24 PM
> To: solr-user@lucene.apache.org
> Subject: Question regarding XSLT
> 
> Hello,
> I currently have set up Solr working and I am doing tests with the XSL
> stylesheets.
> 
> I had no problem in generating HTML files, but while trying to generate
> Json
> files I noticed something odd..
> 
> I am calling Solr with the following URL:
> http://172.16.0.30:8983/solr/gcontacts/select?q=apache*~&fl=all_text&wt=xs
> lt&tr=example.xsl
> 
> What I noticed is, that no matter what you place in the XSLT file, you
> ALWAYS get
> <?xml version="1.0" encoding="UTF-8"?>
> as the first line of the document. Is there a way to avoid this?
> 
> This is the XSL that I am using:
> 
> <xsl:stylesheet version='1.0'
>     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
> >
>   <xsl:output media-type="text/json; charset=UTF-8" />
> 
>   <xsl:template match='/'>
>       searchAsYouType.handleAjaxResponse({"query":"term",
> "results":[<xsl:apply-templates select="response/result/doc"/>]})
>   </xsl:template>
> 
>   <xsl:template match="doc">
>     <xsl:variable name="pos" select="position()"/>
>         <xsl:apply-templates>
>           <xsl:with-param name="pos"><xsl:value-of
> select="$pos"/></xsl:with-param>
>         </xsl:apply-templates>
>   </xsl:template>
> 
>   <xsl:template match="doc/arr" priority="100">
>         <xsl:for-each select="*">
>             {"name":"<xsl:value-of
> select="."/>","type":"text/html","content":"","moreDetailsUrl":"
> http://somedomain.com/","style":"normal"},
>         </xsl:for-each>
>   </xsl:template>
> 
>   <xsl:template match="*"/>
> 
> </xsl:stylesheet>
> 
> This produces the following text:
> <?xml version="1.0" encoding="UTF-8"?>
>       searchAsYouType.handleAjaxResponse({"query":"term", "results":[
>             {"name":"Apache
> Server","type":"text/html","content":"","moreDetailsUrl":"
> http://somedomain.comn/","style":"normal"},
>  ...
> 
> Since the first line is <?xml version="1.0" encoding="UTF-8"?>, it is
> killing my script.
> 
> did I miss something?
> 
> Thanks for the help.