我正在寻找将参数传递给模板匹配的解决方法。我知道这在XPath中是不允许的,因此我正在寻找一个'B计划'解决方案。如何模仿与参数值调用模板匹配?
这就是我想会的工作:
XSLT的第1部分(2.0):
<xsl:template match="/">
<xsl:for-each select="//Main/PageList/Page">
<xsl:result-document href="{@ID}.xml">
<Page ID="{@ID}">
<xsl:apply-templates select="node()">
<xsl:with-param name="theID" select="@ID"/>
</xsl:apply-templates>
</Page>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
所以相当简单,不用理会使用的标记,它的作用在本质上是通过一个节点,并为每个节点创建一个单一的XML文件。每个节点都以一个ID开头,这是我希望为其他模板提供的这个ID。不幸的是这工作正常进行命名模板,但它并不适用于那些匹配工作(如果我理解正确的理论至少)
所以下面是我想看到的是什么工作:
<!-- identity template -->
<xsl:template match="node()|@*">
<xsl:param name="theID"/>
<xsl:copy>
<xsl:apply-templates select="node()|@*">
<xsl:with-param name="theID" select="$theID"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<!-- lots of templates here doing what I want them to do -->
<!-- one template creating an issue -->
<xsl:template match="@src">
<!-- would be nice to know the current ID, but unfortunately this one stays empty... -->
<xsl:param name="theID"/>
<!-- clean current attribute a bit -->
<xsl:variable name="S1" select="replace(.,'\.\.\/','')"/>
<xsl:attribute name="src">
<xsl:choose>
<xsl:when test="contains($S1,'common')">
<!-- just use current value, don't bother about current ID -->
<xsl:value-of select="$S1"/>
</xsl:when>
<xsl:otherwise>
<!-- use ID parameter -->
<xsl:value-of select="concat($theID,'_',$S1)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
不要看代码,它只是整个文件的一小部分,但其实质是我想从我的第一部分(match =“/”)使用其他模板中的ID参数(match = “@src”),但这似乎相当复杂。
我错过了什么吗?如果我只是运气不好而且确实不可能,那么有没有人会有建议,我可以怎么做?
在此先感谢!
您可以通过这种方式将params传递给模板匹配。可能将模板应用于node()过于泛型,并且会在没有参数的情况下匹配模板。 –
@empo:我试过去除node()部分,但没有真正的区别(尽管可能误解了你的观点,所以不确定这是你的目标)。 'match'部分的参数仍然是空的,所以如果它能工作的话,我确实在这个过程中的某处丢失了它。我会玩一个简单的版本(我目前的XSLT稍微复杂一点,然后提供简化) – Wokoman
我的意思是你可能需要'xsl:apply-templates select =“// @ src”'where @src代替node()。 –