2012-03-12 31 views
2

からXSLTでリストを作成します。以下のリストに対して、最大入力し、私はそれぞれにマッチさせたい私は次のXMLファイル持っている2つの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を使用して」とドキュメントは、パラメータとして渡された上記のリストの種類 『」XMLとされ、』:

<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の各サブは、サブと各タイプからタイプを印刷します。サブと型が一致する場合は、太字にする必要があります。私はこれ欲しい:

<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

//の複数の用途と関係していると思います。

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

注:

は、これであなたのルートテンプレート(match="/")を交換してみてください私がテストしたときに、よく形成され、私の出力を維持するために<html>タグを追加しました。

+0

感謝を!タイプを "sub"から変数に設定し、タイプを "。"で区別するタイプ勝者だった。チャームのように働いた!! – SpockJenkins

+0

@ user972029 - あなたは大歓迎です!いい質問のために+1。 –

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> 
関連する問題