开发者

Exclude first child with XSL-T

开发者 https://www.devze.com 2022-12-25 08:50 出处:网络
What I\'m trying to do is fairly simple, but I can\'t find the way to. I just want to iterate over the children of a node excluding the first child.

What I'm trying to do is fairly simple, but I can't find the way to. I just want to iterate over the children of a node excluding the first child.

For instance, in this XML snippet, I would want all the <bar> elements, except the first one:

<开发者_StackOverflow中文版;foo>
    <Bar>Example</Bar>
    <Bar>This is an example</Bar>
    <Bar>Another example</Bar>
    <Bar>Bar</Bar>
</foo>

There is no common attribute by which I can filter (like an id tag or something similar).

Any suggestions?


You can always use position together with xsl:when.

<xsl:when test="node[position() > 1]">
  <!-- Do my stuff -->
</xsl:when>


/foo/Bar[position() > 1]

For example, in 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" }));
}


/foo/bar[position() > 1]

selects all bar elements with the exception of the first, that are children of the top element, which is foo.

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

selects all bar elements in in any XML document, with the exception of the first bar element in this document.


Using apply-templates:

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

or the same xpath for for-each:

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

精彩评论

暂无评论...
验证码 换一张
取 消