i want use xslt in solr output result in json without specific solr attributes.
{ "techproducts": [ { "id": "gb18030test", "price": "0.0" }, { "id": "solr1000", "price": "0.0" }, { "id": "utf8test", "price": "0.0" } ], "paging": { "count": 3 } }
the test of type of field (numeric or boolean) test=". instance of xs:integer"
in order add or not quotes do not work , generate 500 error.
http://localhost:8090/solr/techproducts/select?q=*&fq=price:0&fl=id,price&wt=xslt&tr=json_paginate.xsl caused by: javax.xml.transform.transformerconfigurationexception: solrres:/xslt/json_paginate.xsl: line 37: attribut 'test' obligatoire manquant.
any suggestions?
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:xs="http://www.w3.org/2001/xmlschema" > <xsl:strip-space elements="*"/> <xsl:output method="text" indent="no" media-type="application/json"/> <xsl:template match='/'> <xsl:text>{"techproducts":[</xsl:text> <xsl:apply-templates select="response/result/doc"/> <xsl:text>],</xsl:text> <xsl:text>"paging":{"count":</xsl:text><xsl:value-of select="response/result/@numfound"/><xsl:text>}}</xsl:text> </xsl:template> <xsl:template match="doc"> <xsl:variable name="pos" select="position()"/> <xsl:if test="position() > 1"> <xsl:text>,</xsl:text> </xsl:if> <xsl:text>{</xsl:text> <xsl:apply-templates> <xsl:with-param name="pos"><xsl:value-of select="$pos"/></xsl:with-param> </xsl:apply-templates> <xsl:text>}</xsl:text> </xsl:template> <xsl:template match="doc/*"> <xsl:if test="position() > 1"> <xsl:text>,</xsl:text> </xsl:if> <xsl:text>"</xsl:text><xsl:value-of select="@name"/><xsl:text>":</xsl:text> <xsl:choose> <!-- if integer, not add quotes --> <xsl:when test=". instance of xs:integer"> <xsl:value-of select="."/> </xsl:when> <!-- if boolean, not add quotes --> <xsl:when test=". instance of xs:boolean"> <xsl:value-of select="."/> </xsl:when> <xsl:otherwise> <xsl:text>"</xsl:text><xsl:value-of select="."/><xsl:text>"</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
to solve problem : have upgraded xslt version in solr http://wiki.apache.org/solr/xsltresponsewriter
, used
<xsl:choose> <!-- if decimal, not add quotes --> <xsl:when test=". castable xs:decimal"> <xsl:value-of select="."/> </xsl:when> <!-- if boolean, not add quotes --> <xsl:when test=". castable xs:boolean"> <xsl:value-of select="."/> </xsl:when> <xsl:otherwise> <xsl:text>"</xsl:text><xsl:value-of select="."/><xsl:text>"</xsl:text> </xsl:otherwise> </xsl:choose>
your goal achieve syntactically correct json data: no quotes numbers , strings "true" , "false". that's attempt handling these data types tells me.
in case following xsl:choose
part should work - @ least hope so, since don't know input data:
xslt-2.0: (*)
<xsl:choose> <!-- if number, not add quotes --> <xsl:when test="number(.)"> <xsl:value-of select="."/> </xsl:when> <!-- if boolean, not add quotes --> <xsl:when test="matches(.,'true|false','i')"> <xsl:value-of select="lower-case(.)"/> </xsl:when> <xsl:otherwise> <xsl:text>"</xsl:text><xsl:value-of select="."/><xsl:text>"</xsl:text> </xsl:otherwise> </xsl:choose>
it should mentioned (see comments) numbers printed without quotation marks. not integers. should ok valid json output.
(*) first version of answer had remarkable amount of errors in apologize (downvoting answer quite appropriate). version shown above has been tested saxon , worked expected.
Comments
Post a Comment