【问题标题】:XSLT Access looping number for template模板的 XSLT 访问循环数
【发布时间】:2017-11-30 10:17:24
【问题描述】:

我有如下要求。 示例 XML:

<root>
    <bookseries>
        <author/>
        <version/>
        <book>
            <date>1</date>
            <price>10</price>
        </book>
        <book>
            <date>2</date>
            <price>20</price>
        </book>
        <book>
            <date>3</date>
            <price>30</price>
        </book>
    </bookseries>
</root>

现在我想将模板应用于所有 book 元素。 我目前在我的模板中做的是:

<xsl:template match="//book">
    <!--here I want to see which book element is being copied and also format its data. 
    I am not getting the current order number of template-->
</xsl:template>

目前使用这种方法,我只能重复获取第一本书元素数据 3 次。我需要在各自的模板调用中访问每本书的元素数据。我该怎么做?

【问题讨论】:

  • 那么你在哪里应用你发布的模板,模板正文看起来如何?在正文中,book 元素是上下文节点,因此请使用像 price 这样的相对表达式来访问匹配书籍的价格,而不是 //price,后者将选择所有 price 元素并使用 value-of 和 XSLT 1然后只输出文档中的第一个price元素。

标签: xml xslt xpath xslt-1.0


【解决方案1】:

你可以试试这个:

<xsl:template match="//book">
    <xsl:copy>
       <!-- capturing child with different element name to show only result-->
        <d><xsl:value-of select="date"/></d>
        <p><xsl:value-of select="price"/></p>
    </xsl:copy>
</xsl:template>

当您匹配模板而不是模板内部时,您将捕获名称不为“//”的孩子,在这种情况下,它在整个文件上运行,而不是@martin 在上面的评论中已经提到的当前元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-20
    • 2015-09-26
    • 2012-01-24
    • 2016-11-03
    • 2022-11-07
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多