XSLT 外部文档查找动态值



我在这里浏览了许多问答,但我仍在努力如何开始/完成它(我不精通xslt/xml(。我有一个 XSL 创建一个 XML 源。现在,我还得到了一个XML文件,我需要在其中查找一个品牌,并返回国家和ID。

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. 返回<make_id>... </make_id>的"ID">
  2. 返回 *"_manufactured_in"* 表示<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....)

我感谢任何帮助和提示。

在 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>

最新更新