2013-07-02 42 views
1

我有一個solr字段的問題。當多個節點不適合時,它顯示空字段。由solr在XSLT中匹配

<field name="specs"/> 

在xml原:在XML的solr

<?xml version="1.0" encoding="UTF-8"?> 
<tire xmlns="http://schema.grabber" xmlns:x="http://www.w3.org/1999/xhtml" 
    tire-type="2" product-type="tire" id="102694" trademark="dunlop" 
    season="3" width="130" height="70" wheels="12" load="62" speed="l" 
    host="norauto" model="d207" hostDetailId="102694" hostDbID="6"> 
    <url>product/_102694.html</url> 
    <price>49.95</price> 
    <ecorate>1.15</ecorate> 
    <currency>€</currency> 
    <vat>true</vat> 
    <img>images_produits/650x650/dunlop-d207-runscoot.jpg</img> 
    <content>DUNLOP D207 RUNSCOOT</content> 
    <specs> 
    <spec name="b_xl">0</spec> 
    </specs> 
</tire> 

變換XSLT,這是的solr的XSLT:

 <xsl:template match="/"> 
     <docs> 
      <xsl:apply-templates select="cb:tire|cb:products" /> 
     </docs> 
     </xsl:template> 
     <xsl:template match="cb:tire"> 
     <doc> 
      <xsl:apply-templates select="@*|*" /> 
     </doc> 
     </xsl:template> 
     <xsl:template match="cb:products"> 
     <xsl:apply-templates select="@*|*" /> 
     </xsl:template> 
     <xsl:template match="*/*[@name and not(parent::cb:products)]"> 
     <xsl:call-template name="field"> 
      <xsl:with-param name="name" select="concat(name(),'_',@name)"/> 
     </xsl:call-template> 
     </xsl:template> 
     <xsl:template match="*/*[not(@name) and not(parent::cb:products)]"> 
     <xsl:call-template name="field"/> 
     </xsl:teplate> 
     <xsl:template match="*[parent::cb:products]"> 
     <xsl:choose> 
      <xsl:when test="not(text())"> 
      <doc> 
      <xsl:apply-templates select="*|@*"/> 
      </doc> 
      </xsl:when> 
      <xsl:otherwise> 
      <xsl:call-template name="field"/> 
      </xsl:otherwise> 
     </xsl:choose> 
     </xsl:template> 

    <xsl:template match="*|@*"> 
    <xsl:call-template name="field"> 
     <xsl:with-param name="value" select="."/> 
    </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="field"> 
    <xsl:param name="name" select="name()" /> 
    <xsl:param name="value" select="text()" /> 
     <field name="{translate(lower-case($name),' ','_')}"> 
     <xsl:value-of select="$value" /> 
     </field> 
    </xsl:template> 

     <xsl:template match="text()"/> 
    </xsl:stylesheet> 

問題是元件<specs><spec name="b_xl">0</spec></specs>,該字段爲空,則結果不正確。

這是結果XML:

<docs> 
    <doc> 
    <field name="tire-type">1</field> 
    <field name="product-type">tire</field> 
    <field name="season">1</field> 
    <field name="id">135-80-r13-70-t-kingstar-sk70</field> 
    <field name="trademark">kingstar</field> 
    <field name="model">sk70</field> 
    <field name="width">135</field> 
    <field name="height">80</field> 
    <field name="wheels">13</field> 
    <field name="load">70</field> 
    <field name="speed">t</field> 
    <field name="host">tires</field> 
    <field name="hostdetailid">135-80-r13-70-t-kingstar-sk70</field> 
    <field name="hostdbid">1000</field> 
    <field name="url">135-80-r13-70-t-kingstar-sk70.html</field> 
    <field name="price">29.73</field> 
    <field name="currency">€</field> 
    <field name="vat">true</field> 
    <field name="img">media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/0/1/0181050080001.png</field> 
    <field name="content">135/80 R13 70 T KINGSTAR SK70</field> 
    <field name="specs" /> 
    </doc> 
</docs> 

我需要它是否包含規格顯示元素的內容。

+0

您的XSLT尚未完成。指定的模板字段丟失。 –

+0

對不起,我編輯過。 –

回答

1

要處理的領域,你可以添加一個模板像這樣規格的孩子:

<xsl:template match="cb:specs" priority="1"> 
    <xsl:apply-templates /> 
</xsl:template> 

將產生以下領域:

<field name="spec_b_xl">0</field> 

我不知道這是否是如預期,因爲你沒有告訴我們規格的輸出應該是什麼樣子。

+0

這個xslt是通用的,很難爲我執行。謝謝。 –

+0

我不明白你的意思是「這個xslt是通用的」。你的意思是說,「cb:specs」id不是唯一有子節點的元素,應該考慮嗎? –