2013-01-08 48 views
0

我有一个xpath集合来搜索第一个和第二个标题,但第二个出现为空白,即时猜测这一定很容易,但是我试着改变路径名很多次,我不能改变似乎解决了这个问题,然后我用了一个每个找出是否我的XML文件不正确,但我的循环似乎很容易找到。x路径不算第二个元素

<?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> 

IM试图得到一个菜单,但现在让所有的IM是菜单的第1部分,在for循环,我可以得到部分2.A替代的方法是坚持使用循环,因为我“M循环的循环过程中以某种方式改变类(incrment类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> 

我希望这样做有一定意义 当前进入菜单的输出im是第1部分,然后是“”我想要第2部分的位置。然而,在xsl中的每个循环都带有第1部分和第2部分的标题

+0

你能格式化你的代码,使其更具可读性吗? –

回答

3

如何:

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

或者,这应该工作太:

<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 spec

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

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

+0

谢谢@JLRishe这工作得很好。很容易混淆“第一部分”如何通过阅读你的答案而首先能够工作 –

+0

如果你仔细阅读,那就很有道理。 '// _ something _ [_ number_]'匹配它们父元素的第number个子元素的所有'something'。既然'foo:Heading's是它们父代的第一个孩子,'// foo:Heading [1]'匹配它们的_both_,然后''指令选择第一个该文件。 – JLRishe

+0

对我来说,看起来还是有点神奇 - 我知道规范说的是这样,但它并没有说_why_对于//和/ descendant ::来说[数字]的内容是不同的。有什么想法吗? –

相关问题