对同一模板使用不同的“select”(避免重复)

问题描述 投票:0回答:1

这是简化的问题:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/1999/xhtml"
    version="2.0">

    <xsl:param name="foo" select="'test'"/> <!-- 'test' can also be empty '' or whatever -->

    <!-- XHTML validation -->
    <xsl:output method="xml" 
        doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
        indent="yes"
        encoding="UTF-8"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>bar</title>
            </head>
            <body>
                <xsl:choose>

                    <xsl:when test="string-length($foo)=0">
                        <ol>
                            <xsl:for-each select="something/something2"> <!-- SEE THIS? -->
                                <li>
                                    <xsl:call-template name="generic" />
                                </li>
                            </xsl:for-each>
                        </ol>
                    </xsl:when>

                    <xsl:otherwise>
                        <ol>
                            <xsl:for-each select="something/something2[tmp = $foo"]> <!-- SO MUCH REPETITION!! -->
                                <li>
                                    <xsl:call-template name="generic" />
                                </li>
                            </xsl:for-each>
                        </ol>
                    </xsl:otherwise>

                </xsl:choose>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

我认为这很简单:我怎样才能避免这种重复?我尝试使用xsl:if设置xsl:variable,但后来我不能使用if之外的变量,所以它变得无用。

同样地,我想只在<ol>上应用<xsl:if test="count($varYouMightFigureOut) > 1">,否则,它应该只是自己调用的<xsl:call-template name="generic" />for-each<li>变得无关紧要,不应该显示)。再说一次:一个简单的解决方案涉及很多重复,但我宁愿避免这样的事情。

有任何想法吗?

谢谢!

xml xslt xpath xhtml
1个回答
0
投票

你可以通过完全接受XSLT的内在美来避免这种重复。 使用模板功能,如以下示例中所示:

将中央模板简化为其基本要素:

<xsl:template match="/">
    <html>
        <head>
            <title>bar</title>
        </head>
        <body>
            <xsl:apply-templates />
        </body>
    </html>
</xsl:template>

并在假设的根IF元素上添加两个实现最后<root>条件的子模板(其名称必须与XML根元素的名称相同)

<xsl:template match="root">
   <xsl:call-template name="generic" />
</xsl:template> 

<xsl:template match="root[$varYouMightFigureOut > 1]">
   <ol>
       <xsl:apply-templates />
   </ol>
</xsl:template> 

然后将两个xsl:choose可能性浓缩为这两个模板:

<xsl:template match="something/something2[string-length($foo)=0]">
   <li>
       <xsl:call-template name="generic" />
   </li>
</xsl:template> 

<xsl:template match="something/something2[tmp = $foo]">
   <li>
       <xsl:call-template name="generic" />
   </li>
</xsl:template> 

(如有必要,您可以向xsl:call-template添加参数)。

并使用上述模板使用的占位符“通用”模板完成此操作:

<xsl:template name="generic">
  <xsl:value-of select="normalize-space(.)" />
</xsl:template> 

该解决方案避免了所有不必要的重复,并利用了XSLT的强大功能。

© www.soinside.com 2019 - 2024. All rights reserved.