xml xsl引用列表

时间:2015-07-27 09:52:51

标签: xml xslt

我想用xsl遍历所有"项目" in" Cars"。

如果"项目" in" Cars"有一个元素"值" 我想显示该列表的所有值" CARS"这是定义的 在"列表"再次作为" item" listID =" CARS"。

所以这里它将是0和1.

这可以用xslt完成吗?

<Module>  
   <Lists>
      <item listID="CARS">
         <Description>Features</Description>
         <ElementValue elementID="ACTIVE" value="0">
         <Description></Description>
         </ElementValue>

         <ElementValue elementID="INACTIVE" value="1">
         <Description></Description>
         </ElementValue>
      </item>   
   </Lists>

   <Cars>  
      <item>
         <Name>Bounty</Name>
         <Values listRef="CARS"></Values>
      </item>
   </Cars>  
</Module>    

1 个答案:

答案 0 :(得分:0)

对于交叉引用,您可以定义键<xsl:key name="by-id" match="Module/Lists/item" use="@listID"/>,然后定义模板

<xsl:template match="Cars/item">
  <xsl:variable name="referenced-items" select="key('by-id', Values/@listRef)"/>
  ...
</xsl:template>

可以使用key函数引用其他项目。

这是一个完整的例子:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output method="html" version="5.0" indent="yes" />

    <xsl:key name="by-id" match="Module/Lists/item" use="@listID"/>

    <xsl:template match="Module">
        <div>
            <h1>Module</h1>
            <xsl:apply-templates select="Cars"/>
        </div>
    </xsl:template>

    <xsl:template match="Cars">
        <ul>
            <xsl:apply-templates/>
        </ul>
    </xsl:template>

    <xsl:template match="Cars/item">
        <li>
            <span><xsl:value-of select="Name"/></span>
            <xsl:variable name="referenced-items" select="key('by-id', Values/@listRef)"/>
            <xsl:if test="$referenced-items">
                <ul>
                    <xsl:apply-templates select="$referenced-items/ElementValue/@value"/>
                </ul>
            </xsl:if>
        </li>
    </xsl:template>

    <xsl:template match="Lists/item/ElementValue/@value">
        <li>
            <xsl:value-of select="."/>
        </li>
    </xsl:template>
</xsl:transform>

它在http://xsltransform.net/3NzcBu2在线并输出HTML

<div>
   <h1>Module</h1>
   <ul>  

      <li><span>Bounty</span><ul>
            <li>0</li>
            <li>1</li>
         </ul>
      </li>

   </ul>
</div>

显示为

   

模块

   
          
  • 富饶
                  
    • 0
    •             
    • 1
    •          
          
  •    
相关问题