代码之家  ›  专栏  ›  技术社区  ›  zneak

使用xsl-t排除第一个子项

  •  10
  • zneak  · 技术社区  · 15 年前

    我想做的很简单,但我找不到方法。我只想迭代一个节点的子节点,不包括第一个子节点。

    例如,在这个XML片段中,我希望 <bar> 元素,除了第一个元素:

    <foo>
        <Bar>Example</Bar>
        <Bar>This is an example</Bar>
        <Bar>Another example</Bar>
        <Bar>Bar</Bar>
    </foo>
    

    没有可供筛选的通用属性(如 id 标签或类似的东西)。

    有什么建议吗?

    4 回复  |  直到 12 年前
        1
  •  11
  •   Oded    15 年前

    你可以一直使用 position xsl:when

    <xsl:when test="node[position() > 1]">
      <!-- Do my stuff -->
    </xsl:when>
    
        2
  •  4
  •   Elisha    15 年前
    /foo/Bar[position() > 1]
    

    例如,在c:

    [Test]
    public void PositionBasedXPathExample()
    {
        string xml = @"<foo>
                         <Bar>A</Bar>
                         <Bar>B</Bar>
                         <Bar>C</Bar>
                       </foo>";
    
        XDocument xDocument = XDocument.Parse(xml);
        var bars = xDocument.XPathSelectElements("/foo/Bar[position() > 1]")
            .Select(element => element.Value);
    
        Assert.That(bars, Is.EquivalentTo(new[] { "B", "C" }));
    }
    
        3
  •  3
  •   Dimitre Novatchev    15 年前

    /foo/bar[position() > 1]

    全选 bar 元素,第一个元素除外,它是顶级元素的子元素,即 foo .

    (//bar)[position() >1]

    全选 酒吧 任何XML文档中的元素,第一个除外 酒吧 此文档中的元素。

        4
  •  1
  •   esycat    12 年前

    使用 apply-templates :

    <xsl:apply-templates select="foo/Bar[position() > 1]" />
    

    或相同的xpath for-each :

    <xsl:for-each select="foo/Bar[position() > 1]">
        …
    </xsl:for-each>
    
    推荐文章