【问题标题】:XSLT handling same XML node in different ways in different places of the HTML documentXSLT 在 HTML 文档的不同位置以不同的方式处理相同的 XML 节点
【发布时间】:2016-11-08 11:55:19
【问题描述】:

我想在我的 HTML 文档的顶部有一个用于创建目录的模板,并且我希望能够单击该表中的每个项目以获取有关它的更详细信息,即它跳转到文档下方的相应详细部分。

有点像:

目录:

Node1(点击它会带你到下面加粗的 Node1)

节点2

节点3

其他东西... ...

节点1

描述:废话

内容:1.55

版本:1.55

粗略的 XSLT 代码:

// create table of contents
<table border="1">
<tr bgcolor="#9acd32">
    <th style="text-align:left">Name</th>
</tr>
<xsl:for-each select="">
    <xsl:apply-templates select="my_node"/>
</xsl:for-each>

// do other stuff

// create detailed view (code omitted because I don't know how yet)

// template for node
<xsl:template match="my_node">
    <tr>
        <td><xsl:value-of select="../@name"/></td>
    </tr>
</xsl:template>

我的问题是我想处理这个节点两次,但在我的代码中的不同位置,一次我只获取名称,另一次我获取它的所有信息。据我了解,每个节点都有一个模板是 XSLT 中的首选做法。我怎样才能实现我在此处描述的内容?

我是否传递某种布尔参数来确定在模板中采取的操作?还是为父节点编写一个模板,并在第一种情况下遍历到名称?我不确定我是否喜欢其中任何一个。

【问题讨论】:

    标签: html xml xslt


    【解决方案1】:

    使用 modes。大致:

    <xsl:template match="/">
    
        <!--  create table of contents -->
        <table border="1">
            <tr bgcolor="#9acd32">
                <th style="text-align:left">Name</th>
            </tr>
            <xsl:apply-templates select="my_node" mode="toc"/>
        </table>
    
        <!-- do other stuff -->
    
        <!-- create detailed view  -->
        <h1>Details</h1>
        <xsl:apply-templates select="my_node"/>
    </xsl:template>
    
    <xsl:template match="my_node" mode="toc">
        <tr>
            <td><xsl:value-of select="../@name"/></td>
        </tr>
    </xsl:template>
    
    <xsl:template match="my_node">
        <!-- whatever is required for detailed view -->
    </xsl:template>
    

    附:小心混合xsl:for-eachxsl:apply templates。在大多数情况下,您想使用其中一个。

    【讨论】:

    • 这听起来正是我想要的。我浏览了文档,但由于某种原因没有找到。谢谢!
    猜你喜欢
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 2015-03-20
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多