2013-10-08 32 views
0

我有以下模板,它將顯示一個標籤,然後顯示它後面的值。告訴XSLT使用顯式模板而不是通配符

<xsl:template match="*" mode="row"> 
    <xsl:param name="title"/> 
    <tr> 
     <td width="180"> 
      <xsl:value-of select="$title"/>: </td> 
     <td> 
      <xsl:choose> 
       <xsl:when test="./*"> 
        <xsl:for-each select="./*"> 
         <xsl:apply-templates select="."/> 
        </xsl:for-each> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:apply-templates select="."/> 
       </xsl:otherwise> 
      </xsl:choose> 
     </td> 
    </tr> 

在下列情況下調用:

   <xsl:apply-templates select="Details/Detail/DateOfBirth" mode="row"> 
        <xsl:with-param name="title">Date of birth</xsl:with-param> 
       </xsl:apply-templates> 
       <xsl:apply-templates select="Addresses" mode="row"> 
        <xsl:with-param name="title">Address(s)</xsl:with-param> 
       </xsl:apply-templates> 

現在 - 我不希望有我的每個應用模板時指定的標籤名,這應該是能夠由節點確定名稱。

所以我創建模板要應用的每個節點:

<xsl:template match="DateOfBirth"> 
    <xsl:apply-templates select="." mode="row"> 
     <xsl:with-param name="title">Date Of Birth</xsl:with-param> 
    </xsl:apply-templates> 
</xsl:template> 
<xsl:template match="Addresses"> 
    <xsl:apply-templates select="." mode="row"> 
     <xsl:with-param name="title">Address(s)</xsl:with-param> 
    </xsl:apply-templates> 
</xsl:template> 

並與調用這些:

   <xsl:apply-templates select="Details/Detail/DateOfBirth" mode="row"> 
       </xsl:apply-templates> 
       <xsl:apply-templates select="Addresses" mode="row"> 
       </xsl:apply-templates> 

但它應用通配符模板,使標籤空。有沒有辦法讓它更喜歡顯式模板?

回答

0

我認爲它更喜歡通配符模板的原因是,您使用mode =「row」的模板,並且您的新模板規則處於未命名模式。

我會做到這一點通過創造另一種模式,用非常簡單的模板規則:

<xsl:template match="DateOfBirth" mode="label">Date of Birth</xsl:template> 
<xsl:template match="Addresses" mode="label">Address(es)</xsl:template> 

,然後,而不是傳遞參數,你模式=「行」模板可以做

<td width="180"> 
    <xsl:apply-templates select="." mode="label"/> 
</td> 

順便說一句,你的xsl:選擇似乎也適合模板化:

<xsl:template match="*[*]" mode="row"> 
    <tr> 
     <td width="180"> 
      <xsl:apply-templates select="." mode="label"/> 
     </td> 
     <td> 
      <xsl:apply-templates select="*"/> 
     </td> 
    </tr> 
</xsl:template> 

<xsl:template match="*[not(*)]" mode="row"> 
    <tr> 
     <td width="180"> 
      <xsl:apply-templates select="." mode="label"/> 
     </td> 
     <td> 
      <xsl:apply-templates select="."/> 
     </td> 
    </tr> 
</xsl:template> 
0

這是我做過黑客:

<xsl:template match="*" mode="row"> 
    <xsl:param name="title"/> 
    <xsl:choose> 
     <xsl:when test="$title=''"> 
      <xsl:apply-templates select="."/> 
     </xsl:when> 
     <xsl:otherwise> 
      <tr> 
       <td width="180"> 
        <xsl:value-of select="$title"/>: </td> 
       <td> 
        <xsl:choose> 
         <xsl:when test="./*"> 
          <xsl:for-each select="./*"> 
           <xsl:apply-templates select="."/> 
          </xsl:for-each> 
         </xsl:when> 
         <xsl:otherwise> 
          <xsl:apply-templates select="."/> 
         </xsl:otherwise> 
        </xsl:choose> 
       </td> 
      </tr> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

它檢查是否標題是空白的,如果是(你調用它的第一次),然後將它熄滅,找到了明確的模板該節點然後用標題重新調用它。