获取特定节点的父节点及其子节点

发布于 2024-11-03 22:00:49 字数 1270 浏览 2 评论 0原文

我有一个如下所示的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一城柳絮吹成雪 2024-11-10 22:00:49

您想要选择具有子 PagePage 元素,其 iscurrent 属性等于 true

$sitemap/Page[Page[@iscurrent='true']]

演示:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="sitemap" select="/Page" />
    <xsl:template match="/">
        <xsl:apply-templates select="$sitemap/Page[Page[@iscurrent='true']]" />
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

在此输入:

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

输出:

<Page Title="Communities" Depth="2">
    <Page Title="Blog" iscurrent="true" Depth="3" />
    <Page Title="Something" Depth="3" />
    <Page Title="Anything" Depth="3" />
</Page>

You want something that selects the Page element having a child Page whose iscurrent attribute equals true:

$sitemap/Page[Page[@iscurrent='true']]

Demonstration:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="sitemap" select="/Page" />
    <xsl:template match="/">
        <xsl:apply-templates select="$sitemap/Page[Page[@iscurrent='true']]" />
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

On this input:

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

Output:

<Page Title="Communities" Depth="2">
    <Page Title="Blog" iscurrent="true" Depth="3" />
    <Page Title="Something" Depth="3" />
    <Page Title="Anything" Depth="3" />
</Page>
通知家属抬走 2024-11-10 22:00:49

XPath 是

   /Page[*[@iscurrent='true']]

'* 选择后代和自身。

The XPath is

   /Page[*[@iscurrent='true']]

The '* selects descendents and self.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文