x路径不计算第二个元素 [英] x path not counting second element

查看:90
本文介绍了x路径不计算第二个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xpath设置用于搜索第一个和第二个标题,但是第二个标题是空白的,即时猜测这一定很容易,但是我尝试了多次更改路径名称,我似乎无法解决它,我然后使用一个为每个发现如果可能我的XML文件不正确,但我的循环似乎很容易找到。

i have a xpath set to search for the first and second heading however the second comes up as blank, im guessing this must be pretty easy however ive tried changing the path names so many times and i cant seem to solve it, i then used a for each to find out if maybe my xml file was incorrect but my loop seems to find both with ease.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:foo="blogschema.xsd"
exclude-result-prefixes="foo">
<xsl:output method ="html" indent ="no"/> 
<xsl:template match="/">
<html>
<link rel ="stylesheet" type="text/css" href="blogStyle.css"/>
    <body>
    <ul class="menu">
    <li><a href="#" class ="Part 1"><xsl:value-of select="//foo:Heading[1]"/> </a></li>
    <li><a href="#" class ="Part 2"><xsl:value-of select="//foo:Heading[2]"/></a></li>
    </ul>






        <div id="heading">

        <xsl:for-each select="//foo:Entry">
        <!--<xsl:sort select="Heading"/> -->


        <div class ="topNavigation"><a href="#home"><h3><xsl:value-of    select="foo:Heading"/></h3></a></div>

        </xsl:for-each>
        <div class ="panes">




        </div>

        </div>
        <div id ="blogpicture">
            <div class="picture">
                <div id="headerTitle"><h2><xsl:value-of        select="//foo:Title"/></h2></div>
                </div>
                </div>


    </body>
    </html>
</xsl:template>
</xsl:transform>
>

< xsl:for-each select =// foo:Entry>
<! - < xsl:sort select =Heading/> - >


< div class =topNavigation>< a href =#home>< h3>< xsl:value-of select =foo:Heading />< / H3>< / A>< / DIV>

< / xsl:for-each>
< div class =panes>




< / div>

< / div>
< div id =blogpicture>
< div class =picture>
< div id =headerTitle>< h2>< / div>< xsl:value-of select =// foo:Title/>< / h2>< / div>
< / div>
< / div>


< / body>
< / html>
< / xsl:template>
< / xsl:transform>

即时通讯试图获得一个菜单,但现在所有即时消息都是菜单的第一部分, for循环,我可以得到第2部分。替代方案可以坚持使用循环,并且当循环在循环期间以某种方式改变类(包含类id)






我的博客

<Entry> <Heading id="101">Part 1</Heading> <body> <text>something interesting</text> <pictures>pictures in the body</pictures> <videos>Videos in the body</videos> </body> <labels>Seperate labels with commas</labels> <date> 20121119</date> <location>The location the blog was published</location> </Entry> <Entry> <Heading id="102">Part 2</Heading> <body> <text>something</text> <pictures>pictures in the body</pictures> <videos>Videos in the body</videos> </body> <labels>Seperate labels with commas</labels> <date> 2012-11-26Z</date> <location>The location the blog was published</location> </Entry> <author>me</author>

我希望这样做有一定意义
当前在菜单中输入的输出是第1部分,然后是第2部分的输入。在xsl中的每个循环都带有第1部分和第2部分的标题$ / $ $

解决方案
How about this:

推荐答案

li>< a href =#class =Part 1>< xsl:value-of select =// foo:Entry [1] / foo:Heading/> < / A>< /锂>
< li>< a href =#class =Part 2>< xsl:value-of select =// foo:Entry [2] / foo:Heading/> < / A>< /锂>

Alternatively, this should work too:

或者,这也应该可行:

<$ < xsl:value-of select =(// foo:Heading)[1] p $ p> < li>< a href =#class =Part 1> /> < / A>< /锂>
< li>< a href =#class =Part 2>< xsl:value-of select =(// foo:Heading)[2]/>< / A>< /锂>

<li><a href="#" class ="Part 1"><xsl:value-of select="(//foo:Heading)[1]"/> </a></li> <li><a href="#" class ="Part 2"><xsl:value-of select="(//foo:Heading)[2]"/></a></li>

从全能 XPath规范

From the almighty XPath spec:


/ / / descendant-or-self :: node()/ 的缩写。例如, // para / descendant-or-self :: node()/ child :: para 因此将选择文档中的任何段元素(即使作为文档元素的para元素将由 // para >来选择,因为文档元素节点是根节点); div // para div / descendant-or-self :: node()/ child :: para 所以会选择div子的所有para子代。

// is short for /descendant-or-self::node()/. For example, //para is short for /descendant-or-self::node()/child::para and so will select any para element in the document (even a para element that is a document element will be selected by //para since the document element node is a child of the root node); div//para is short for div/descendant-or-self::node()/child::para and so will select all para descendants of div children.

注意:位置路径 // para [1] 不是 与位置路径 / descendant :: para [1] 相同。后者选择第一个后代para元素;前者选择父母的第一个孩子的所有后代para元素。

NOTE: The location path //para[1] does not mean the same as the location path /descendant::para[1]. The latter selects the first descendant para element; the former selects all descendant para elements that are the first para children of their parents.

这篇关于x路径不计算第二个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆