2013-07-28 90 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

使用不完整的片段很难。我在你编辑的问题中看到你现在使用'',在哪个上下文中你用那个?在浏览器内部,同一个来源策略可能会阻止任何访问。 –