2013-07-28 23 views
2

我已經查看了一些Q & A的在這裏,但我仍然在努力如何開始/完成它(我不太熟悉xslt/xml)。我有一個XSL創建一個XML提要。此外,我已經獲得了一個XML文件,我需要查找一個品牌,並返回國家和ID。XSLT外部文檔查找動態值

的brand_country.xml文件的內容:

<Make> 
    <man_id>22</man_id> 
    <man_name>Bentley</man_name> 
    <man_country>Britain</man_country> 
    <_type_>car</_type_> 
</Make> 
<Make> 
    <man_id>23</man_id> 
    <man_name>Benz</man_name> 
    <man_country>Germany</man_country> 
    <_type_>car</_type_> 
</Make> 
<Make> 
    <man_id>24</man_id> 
    <man_name>Berkley</man_name> 
    <man_country>Britain</man_country> 
    <_type_>car</_type_> 
</Make> 
<Make> 
    <man_id>25</man_id> 
    <man_name>Bitter</man_name> 
    <man_country>Germany</man_country> 
    <_type_>car</_type_> 
</Make> 
<Make> 
    <man_id>28</man_id> 
    <man_name>BMW</man_name> 
    <man_country>Germany</man_country> 
    <_type_>car</_type_> 
</Make> 

現在在我的XSL我已經

<xsl:for-each select="entries/entry"> 

<root> 
    <channel> 
    <ad> 
    <category_id>1</category_id> 
    <ad_id><xsl:value-of select="id" /></ad_id> 
    <locale>en</locale> 
    <country>n/a</country> 
    <make_id><xsl:value-of select="fields/field_make/data" /> </make_id> 
    <year><xsl:value-of select="fields/field_year/data" /></year> 
    <handling><xsl:value-of select="fields/field_lhdrhd/data" /></handling> 
    <heading><xsl:value-of select="fields/field_make/data" /> <xsl:text> </xsl:text> <xsl:value-of select="name" /></heading> 
    <reg_no> </reg_no> 
    <chassis_no><xsl:value-of select="fields/field_chassis_nr/data" /></chassis_no> 
    <engine_no> </engine_no> 

    <price_type> 
    <xsl:choose> 
     <xsl:when test="string-length(fields/field_price_poa/data)"> 
     <xsl:text>POA</xsl:text> 
     </xsl:when> 
     <xsl:otherwise>Asking Pricefix</xsl:otherwise> 
    </xsl:choose> 
    </price_type> 
    <price> 
    <xsl:choose> 
     <xsl:when test="string-length(fields/field_price_poa/data)"> 
     <xsl:text> </xsl:text> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="fields/field_price/data" /> 
     </xsl:otherwise> 
    </xsl:choose> 
    </price> 


    <currency_id> 
    <xsl:choose> 
     <xsl:when test="fields/field_currency/data = 'GBP'"> 
     <xsl:text>20</xsl:text> 
     </xsl:when> 
     <xsl:when test="fields/field_currency/data = 'USD'"> 
     <xsl:text>10</xsl:text> 
     </xsl:when>    
    </xsl:choose>  
    </currency_id> 

    </ad> 

    </channel> 
</root> 
</xsl:for-each> 

這正是我所需要的電流供給。

現在,我將需要使用的內容...

<xsl:value-of select="fields/field_make/data" /> 

...在brand_country.xml文件查找,並發現:

  1. 回報「ID」<make_id>... </make_id>
  2. 返回*「_製造商」*爲<country> ... </country>

這是我到目前爲止有:

(cut....) 
    <xsl:key name="mancountry" match="man_country" use="../man_name"/> 
    <xsl:key name="manid" match="man_id" use="../man_name"/> 

<xsl:template match="/section|/category|/entry_details"> 

    <xsl:for-each select="entries/entry"> 
    <root> 
    <channel> 
    <ad> 
     <category_id>1</category_id> 
     <ad_id><xsl:value-of select="id" /></ad_id> 
     <locale>en</locale> 
     <xsl:variable name="inputmake" select="fields/field_make/data"/> 

     <country> 
     <xsl:for-each select="document('http://www.xxx.yyy/dev/feed_data/brand_country.xml')"> 
      <xsl:variable name="value" select="key('mancountry',$inputmake)"/> 
      <xsl:choose> 
       <xsl:when test="$value"> 
       <xsl:value-of select="$value"/> 
       </xsl:when> 
       <xsl:otherwise>world</xsl:otherwise> 
      </xsl:choose> 
      </xsl:for-each> 
     </country> 
(cut....) 

我感謝所有幫助和提示。

回答

1

使用XSLT 2.0把下面的頂級代碼的xsl:stylesheet內:

<xsl:param name="lk" select="'brand_country.xml'"/> 
<xsl:variable name="lk-doc" select="doc($lk)"/> 

<xsl:key name="brand" match="Make" use="man_name"/> 

現在查找值簡單地使用

<xsl:variable name="make" select="key('brand', fields/field_make/data, $lk-doc)"/> 

分別

<country><xsl:value-of select="$make/man_country"/></country> 

使用XSLT 1.0你可以使用

<xsl:param name="lk" select="'brand_country.xml'"/> 
<xsl:variable name="lk-doc" select="document($lk)"/> 

<xsl:key name="brand" match="Make" use="man_name"/> 

然後

<xsl:variable name="this" select="."/> 

<xsl:for-each select="$lk-doc"> 
    <xsl:variable name="make" select="key('brand', $this/field_make/data)"/> 
    <country><xsl:value-of select="$make/man_country"/></country> 
</xsl:for-each> 
+0

感謝您的快速回復,我會盡量讓你知道 – LiteBit

+0

似乎並不TE工作:(國標記現在沒有在生成的XML顯示在所有(使用XSLT 1.0解決方案) – LiteBit

+0

使用不完整的片段很難。我在你編輯的問題中看到你現在使用'',在哪個上下文中你用那個?在瀏覽器內部,同一個來源策略可能會阻止任何訪問。 –