2012-03-12 85 views
2

用XSLT創建列表,我有以下XML文件:從兩個XML文檔

<root> 
    <sub type="print">print</sub> 
    <sub type="email">email</sub> 
</root> 

我想匹配每個對下面的列表中鍵入了:

<types> 
    <type>email</type> 
    <type>broadcast</type> 
    <type>mobile</type> 
    <type>print</type> 
    <type>web</type> 
</types> 

使用此XSLT與「 doc「是xml和」types「上面列表中傳入的參數:

<xsl:stylesheet 
    xmlns = "http://www.w3.org/1999/xhtml" 
    xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" 
    version="2.0"> 

    <xsl:output method="xhtml" indent="yes" /> 

    <xsl:param name="doc"/> 
    <xsl:param name="types"/> 

    <xsl:template match="/"> 
    <xsl:for-each select="$doc//sub"> 
     <xsl:variable name="count"> 
     <xsl:number value="position()" format="1"/> 
     </xsl:variable> 

     <ul> 
     <xsl:for-each select="$types//type"> 
      <xsl:choose> 
      <xsl:when test="$doc//sub[$count]/@type = text()"> 
       <li> 
       <b> 
        <xsl:value-of select="$doc//sub[$count]/@type"/> - <xsl:value-of select="text()"/> 
       </b> 
       </li> 
      </xsl:when> 
      <xsl:otherwise> 
       <li> 
       <xsl:value-of select="$doc//sub[$count]/@type"/> - <xsl:value-of select="text()"/> 
       </li> 
      </xsl:otherwise> 
      </xsl:choose> 
     </xsl:for-each> 
     </ul> 
    </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

這應該給我一個無序列表f或者我的xml中的每個sub都從sub打印,然後打印每個類型的類型。當子和類型匹配時,它應該是粗體。我想這一點:

<ul> 
    <li>print - email</li> 
    <li>print - broadcast</li> 
    <li>print - mobile</li> 
    <li><b>print - print</b></li> 
    <li>print - web</li> 
</ul> 
<ul> 
    <li><b>email - email</b></li> 
    <li>email - broadcast</li> 
    <li>email - mobile</li> 
    <li>email - print</li> 
    <li>email - web</li> 
</ul> 

但我得到這個:

<ul> 
    <li><b>print email - email</b></li> 
    <li>print email - broadcast</li> 
    <li>print email - mobile</li> 
    <li><b>print email - print</b></li> 
    <li>print email - web</li> 
</ul> 
<ul> 
    <li><b>print email - email</b></li> 
    <li>print email - broadcast</li> 
    <li>print email - mobile</li> 
    <li><b>print email - print</b></li> 
    <li>print email - web</li> 
</ul> 

感謝任何及所有的幫助。

回答

1

我認爲這可能與//的多種用途有關。

嘗試用這種替代你的根模板(match="/"):

<xsl:template match="/"> 
    <html> 
     <xsl:for-each select="$doc/root/sub"> 
     <xsl:variable name="vType" select="@type"/> 
     <ul> 
      <xsl:for-each select="$types/types/type"> 
      <li> 
       <xsl:choose> 
       <xsl:when test=".=$vType"> 
        <b> 
        <xsl:value-of select="concat($vType,' - ',.)"/> 
        </b> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:value-of select="concat($vType,' - ',.)"/> 
       </xsl:otherwise> 
       </xsl:choose>    
      </li> 
      </xsl:for-each> 
     </ul> 
     </xsl:for-each>  
    </html> 
    </xsl:template> 

注:我添加了<html>標籤,以保持我的輸出測試時良好的。

+0

謝謝!將類型從「sub」設置爲變量,然後使用「。」將類型與「類型」區分開來。是一個勝利者。像魅力一樣工作! – SpockJenkins 2012-03-12 17:50:22

+0

@ user972029 - 非常歡迎您! +1是一個很好的問題。 – 2012-03-12 18:10:20

0

鑑於你的子文件作爲主要輸入和類型subtypes.xml

<xsl:stylesheet version="2.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 


<xsl:template match="sub"> 
<ul> 
    <xsl:apply-templates select="doc('subtypes.xml')"> 
    <xsl:with-param name="this" select="." tunnel="yes"/> 
    </xsl:apply-templates> 
</ul> 
</xsl:template> 

<xsl:template match="type"> 
<xsl:param name="this" tunnel="yes"/> 
<li> 
    <xsl:choose> 
    <xsl:when test="$this/@type=."> 
    <b><xsl:value-of select="$this/@type,' - ',."/></b> 
    </xsl:when> 
    <xsl:otherwise> 
    <xsl:value-of select="$this/@type,' - ',."/> 
    </xsl:otherwise> 
    </xsl:choose> 
</li> 
</xsl:template> 

</xsl:stylesheet> 

生產:

<?xml version="1.0" encoding="UTF-8"?> 
    <ul> 
    <li>print - email</li> 
    <li>print - broadcast</li> 
    <li>print - mobile</li> 
    <li><b>print - print</b></li> 
    <li>print - web</li> 
</ul> 
    <ul> 
    <li><b>email - email</b></li> 
    <li>email - broadcast</li> 
    <li>email - mobile</li> 
    <li>email - print</li> 
    <li>email - web</li> 
</ul>