XSL - 基本上是这样的:Return 到应用模板的模板

XSL - Basically this: Return to the template that applied a template

这是我的code-situation的描述。实际问题可能会简化为您在标题中找到的问题,以及您在 post 末尾找到的详细信息。但是myabe我找错方向了,所以:

这是一个给定的结构,它是mysqltable的内容转换为xml。

<result>
    <row>
        <col>..</col>
    </row>
    <row>
        <col>..</col>
    </row>
    <row>
        <col>..</col>
    </row>
    <row>
        <col>..</col>
    </row>
    [..]
</result>

现在我正在尝试编写一个 xsl 文件,其中包含有关如何显示这些结构的信息。

基本上是这样的:

<xsl:template match="//result">
    <table>
        <xsl:apply-templates/>
    </table>
</xsl:template>

<xsl:template match="//result/row">
    <tr>
        <xsl:apply-templates/>
    </tr>
</xsl:template>

<xsl:template match="//result/row/col">
    <td>
        <xsl:value-of select="."/>
    </td>
</xsl:template>

这行得通,但我现在的困难是让描述更加可变,它不应该总是 "table",有时我需要 div 或任何其他 html代码到"render"每个部分table.

所以我决定在 xml 文件的另一部分中标记它,如下所示:

<display>
    <MyTable>
        <MyRow>
            <MyCol/>
        </MyRow>
    </MyTable>
</display>

它也可以是这样的:

<display>
    <MyDiv>
        <MyDivSub>
            <MyDivSubSub/>
        </MyDivSub>
    </MyDiv>
</display>

但是,这些声明在 xsl 文件中是这样定义的:

<xsl:template match="//display/MyTable">
    <table>
        <xsl:apply-templates/>
    </table>
</xsl:template>

<xsl:template match="//display/MyTable/MyRow">
    <tr>
        <xsl:apply-templates/>
    </tr>
</xsl:template>

<xsl:template match="//display/MyTable/MyRow/MyCol">
    <td>
        <xsl:value-of select="."/>
    </td>
</xsl:template>

现在,我的计划是为结果、行、列设计模板,它们只是调用在 xml 文件的显示元素中指定的模板。甚至可以是这样的:

<xsl:template match="//result">
    <xsl:apply-templates select="//Display/*">
</xsl:template>

<xsl:template match="//result/row">
    <xsl:apply-templates select="//Display/*/*">
</xsl:template>

<xsl:template match="//result/row/col">
    <xsl:apply-templates select="//Display/*/*/*">
</xsl:template>

当然在这一点上这是行不通的,因为应用的模板在 apply-templates 内部时不会返回到结果。相反,他们将它们应用于显示部分。这可以通过传递参数来解决。我试过了,它有效。但对我来说,包含最终描述信息的模板(如 MyTable、MyRow..)不带有这些额外行的 "messed up" 是很重要的。无论如何,以后它们可能会变得相当复杂,并且可能有很多这样的类型,所以我希望它们的基本结构尽可能小。换句话说:我正在寻找的神奇功能就是这个,放在 Depiction-Templates:

<xsl:template match="//display/MyTable">
    <table>
        <xsl:"GO BACK TO THE TEMPLATE THAT CALLED YOU"/>
    </table>
</xsl:template>

是否有类似的功能,或者有人知道我的问题的其他解决方案吗?还是我必须接受带有参数的解决方案?我不敢相信,因为实际上它并没有那么复杂的过程。

我希望我说清楚了,如果有任何问题,请提问。米

我真的不认为多一行来传递参数会"mess them up"。我认为没有办法完全按照您的描述进行操作,即使有,也没有任何意义。返回调用模板不会导致无限循环吗?

下面有什么严重的问题吗?我认为它非常干净和灵活:

<xsl:template match="MyTable">
    <xsl:param name="context" select="/.." />
    <table>
        <xsl:apply-templates select="$context/node()" />
    </table>
</xsl:template>

<xsl:template match="MyRow">
    <xsl:param name="context" select="/.." />
    <tr>
        <xsl:apply-templates select="$context/node()" />
    </tr>
</xsl:template>

<xsl:template match="MyCol">
    <xsl:param name="context" select="/.." />
    <td>
        <xsl:value-of select="$context"/>
    </td>
</xsl:template>

<xsl:variable name="display" select="(//display)[1]" />

<xsl:template match="result">
    <xsl:apply-templates select="$display/*">
        <xsl:with-param name="context" select="." />
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="result/row">
    <xsl:apply-templates select="$display/*/*">
        <xsl:with-param name="context" select="." />
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="result/row/col">
    <xsl:apply-templates select="$display/*/*/*">
        <xsl:with-param name="context" select="." />
    </xsl:apply-templates>
</xsl:template>