获取特定节点的父节点及其子节点
我有一个如下所示的 XML 结构:
<Page Depth="1">
<Page Title="Communities" Depth="2">
<Page Title="Blog" iscurrent="true" Depth="3"/>
<Page Title="Something" Depth="3"/>
<Page Title="Anything" Depth="3"/>
</Page>
<Page Title="News" Depth="2">
<Page Title="Archived news" Depth="3"/>
</Page>
</Page>
我已将其放入名为 sitemap
的 XML 变量中
我正在努力使用 XPATH,该变量将返回带有 @iscurrent
的节点的父节点code> 属性及其子属性。因此,从上面的示例中,我需要以下子集:
<Page Title="Communities" Depth="2">
<Page Title="Blog" iscurrent="true" Depth="3"/>
<Page Title="Something" Depth="3"/>
<Page Title="Anything" Depth="3"/>
</Page>
我已尝试应用此模板,但没有任何输出:
<xsl:apply-templates mode="Tabs"
select="$sitemap/Page[@iscurrent='true']/parent/*" />
该模板不应该相关,但无论如何,这是它的基本版本:
<xsl:template mode="Tab" match="*">
<li>
<xsl:if test="@iscurrent = 'true'">
<xsl:attribute name="class">current</xsl:attribute>
</xsl:when>
<xsl:value-of select="@MenuTitle" />
</li>
</xsl:template>
I have an XML structure like the following:
<Page Depth="1">
<Page Title="Communities" Depth="2">
<Page Title="Blog" iscurrent="true" Depth="3"/>
<Page Title="Something" Depth="3"/>
<Page Title="Anything" Depth="3"/>
</Page>
<Page Title="News" Depth="2">
<Page Title="Archived news" Depth="3"/>
</Page>
</Page>
I have put this into an XML Variable called sitemap
I'm struggling with XPATH that would return the parent node of the node with the @iscurrent
attribute along with its children. So from the example above, I need the following subset:
<Page Title="Communities" Depth="2">
<Page Title="Blog" iscurrent="true" Depth="3"/>
<Page Title="Something" Depth="3"/>
<Page Title="Anything" Depth="3"/>
</Page>
I have tried applying this template, but nothing gets output:
<xsl:apply-templates mode="Tabs"
select="$sitemap/Page[@iscurrent='true']/parent/*" />
The template shouldn't be relevant, but anyhow, here is a basic version of it:
<xsl:template mode="Tab" match="*">
<li>
<xsl:if test="@iscurrent = 'true'">
<xsl:attribute name="class">current</xsl:attribute>
</xsl:when>
<xsl:value-of select="@MenuTitle" />
</li>
</xsl:template>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要选择具有子
Page
的Page
元素,其iscurrent
属性等于true
:演示:
在此输入:
输出:
You want something that selects the
Page
element having a childPage
whoseiscurrent
attribute equalstrue
:Demonstration:
On this input:
Output:
XPath 是
'* 选择后代和自身。
The XPath is
The '* selects descendents and self.