Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 使用XSL-T排除第一个子项_Xslt_Xpath_Css Selectors - Fatal编程技术网

Xslt 使用XSL-T排除第一个子项

Xslt 使用XSL-T排除第一个子项,xslt,xpath,css-selectors,Xslt,Xpath,Css Selectors,我想做的事情很简单,但我找不到解决问题的方法。我只想迭代一个节点的子节点,不包括第一个子节点 例如,在这个XML片段中,我需要所有元素,除了第一个: <foo> <Bar>Example</Bar> <Bar>This is an example</Bar> <Bar>Another example</Bar> <Bar>Bar</Bar> </foo

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

例如,在这个XML片段中,我需要所有
元素,除了第一个:

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

例子
这是一个例子
另一个例子
酒吧
没有可以用来过滤的公共属性(比如
id
标记或类似的东西)


有什么建议吗?

您可以随时将
position
xsl:when
一起使用

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

例如,在C#中:

[测试]
public void PositionBasedXPathExample()
{
字符串xml=@“
A.
B
C
";
XDocument XDocument=XDocument.Parse(xml);
var Bar=xDocument.XPathSelectElements(“/foo/Bar[position()>1]”)
.Select(element=>element.Value);
断言(条,Is.EquivalentTo(新[]{“B”,“C”}));
}

使用
应用模板

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

/foo/bar[position()>1]

选择所有
bar
元素,但第一个元素除外,它们是顶部元素的子元素,即
foo

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


选择任何XML文档中的所有
bar
元素,但此文档中的第一个
bar
元素除外。

。。。我觉得自己像个n00b<代码>谢谢。@zneak-我们都在那里。。。太多的东西无法同时保存在大脑中。
node.position()!=1
不是语法正确的XPath表达式。答案仍然包含语法错误的XPath表达式!为什么不试着运行代码并只发布正确的代码呢?示例需要正确才能有用。我从不使用未经测试的代码回答。好问题(+1)。请参阅我的答案以获得完整的解决方案。
[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" }));
}
<xsl:apply-templates select="foo/Bar[position() > 1]" />
<xsl:for-each select="foo/Bar[position() > 1]">
    …
</xsl:for-each>