2013-10-08 28 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> 

它检查是否标题是空白的,如果是(你调用它的第一次),然后将它熄灭,找到了明确的模板该节点然后用标题重新调用它。