告诉XSLT使用显式模板而不是通配符

时间:2013-10-08 04:20:28

标签: 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>

但它正在应用通配符模板,将标签留空。有没有办法告诉它更喜欢显式模板?

2 个答案:

答案 0 :(得分: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>

它检查标题是否为空白,如果是(你是第一次调用它),然后它会关闭并找到该节点的显式模板,然后用标题重新调用它。

答案 1 :(得分:0)

我认为它首选通配符模板的原因是您使用mode =“row”的模板,并且您的新模板规则处于未命名模式。

我会通过使用非常简单的模板规则创建另一种模式来实现此目的:

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

然后代替传递参数,你可以使用mode =“row”模板

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

顺便提一句,你的xsl:choose似乎也适合模板化:

<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>
相关问题