2011-10-31 59 views
1

我正在使用Umbraco并在XSLT中遇到了一些问题。xslt - 获取子节点下的节点类型

我有以下结构:

root 
    nodeA 
     nodeB 
     nodeB 
     nodeB 
    nodeC 
     nodeA 
     nodeB 
     nodeB 
     nodeB 

我希望能够在根或nodeA上nodeC上从下得到了nodeA节点B。

我目前使用以下方法:

<xsl:param name="currentPage"/> 
    <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*/nodeC" /> 
<xsl:template match="/"> 

<!-- start writing XSLT --> 
    <xsl:for-each select="$siteRoot/ancestor-or-self::*/nodeA/nodeB"> 
    <xsl:if test="string(umbracoNaviHide) != '1'"> 
    <li style="background-image: none;"> 
     <xsl:if test="position() = last()"> 
     <xsl:attribute name="class">no-border</xsl:attribute> 
     </xsl:if> 
      <a> 
       <xsl:attribute name="href"> 
       <xsl:value-of select="umbraco.library:NiceUrl(current()/@id)"/> 
       </xsl:attribute> 
       <xsl:attribute name="style"> 
       text-decoration: none; 
       </xsl:attribute> 
       <xsl:value-of select="./Name" /></a></li> 
    </xsl:if> 
    </xsl:for-each> 
</xsl:template> 
+0

@Dimire - 有关XPath的书在阅读列表中。在MVC3之后,实体框架和Objective-C :) –

回答

1
<xsl:param name="currentPage"/> 
    <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*/nodeC" /> 
. . . 
    <xsl:for-each select="$siteRoot/ancestor-or-self::*/nodeA/nodeB"> 

你必须阅读XPath的东西,学习不同轴的意义。

你真正想要这个代码的完全相反:

<xsl:param name="currentPage"/> 
     <xsl:variable name="siteRoot" select="$currentPage/descendant-or-self::nodeC[1]" /> 
    . . . 
    <xsl:for-each select="$siteRoot/nodeA/nodeB"> 

或者,作为一个单一的XPath表达式更好:

$currentPage/*/nodeC[1]/nodeA/nodeB 

注意:只要有可能,应避免使用XPath descendant::descendant-or-self::轴或伪操作符//嘿,导致效率显着下降(执行速度缓慢),并且与[]运营商一起使用时会产生异常和反直觉行为。