【问题标题】:how to show only two items in xslt?如何在 xslt 中只显示两个项目?
【发布时间】:2017-09-01 09:09:48
【问题描述】:

你能告诉我如何显示前两个元素是 xslt 吗? 这是我的代码 http://xsltransform.net/ehVYZMp/6

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:exsl="http://exslt.org/common"
 extension-element-prefixes="exsl">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="a">

      <hmtl>
        <head>
          <title>New Version!</title>
        </head>
          <xsl:apply-templates select="t"/>
      </hmtl>
    </xsl:template>

<xsl:template match="t[@live='2']">
<xsl:value-of select="@b"/>
 </xsl:template>
</xsl:transform>

预期输出:12

【问题讨论】:

  • 您能否编辑您的问题以包含输入 XML? xslttransform 很棒,但目前不可用....谢谢!

标签: xml xslt xslt-1.0 xslt-2.0 xslt-grouping


【解决方案1】:

如果您只想选择前两个 t 元素的 live 属性设置为“2”,则可以将此逻辑放在 xsl:apply-templates 中而不是模板匹配中

<xsl:apply-templates select="t[@live='2'][position() &lt; = 2]"/>

试试这个 XSLT

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:exsl="http://exslt.org/common"
 extension-element-prefixes="exsl">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="a">

      <hmtl>
        <head>
          <title>New Version!</title>
        </head>
          <xsl:apply-templates select="t[@live='2'][position() &lt; = 2]"/>
      </hmtl>
    </xsl:template>

<xsl:template match="t">
<xsl:value-of select="@b"/>
 </xsl:template>
</xsl:transform>

【讨论】:

    猜你喜欢
    • 2020-03-28
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    相关资源
    最近更新 更多