没有孩子的xslt副本

Yis*_*ski 5 xslt attributes nodes

嗨,我有一个看起来像这样的站点地图xml文档

<pagenode title="home" url="~/" fornavbar="true">
 <pagenode title="admin" url="~/admin" fornavbar="false">
  <pagenode title="users" url="~/admin/users" fornavbar="false"/>
  <pagenode title="events" url="~/admin/events" fornavbar="true"/>
 </pagenode>
 <pagenode title="catalog" url="~/catalog" fornavbar="true"/>
 <pagenode title="contact us" url="~/contactus" fornavbar="false"/>
</pagenode>
Run Code Online (Sandbox Code Playgroud)

现在我想检索导航栏的xml文档,其中包含所有具有fornavbar = true的页面节点.如何才能做到这一点?

到目前为止我能得到的最接近的是:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="pagenode[@fornavbar='true']">
  <xsl:copy-of select="."/>
 </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

这个问题包括任何匹配为navbar的孩子

我只想复制所有属性,而不是所有孩子

但如果我试试

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="pagenode[@fornavbar='true']">
  <pagenode title="{@title}"  url="{@url}"/>
  <xsl:apply-templates/>
 </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

那我有两个问题

  1. 我可能会分别输入每个属性,每页都有很多属性,最终它们很容易改变
  2. 它失去了等级制度.一切都变得平坦

我会感谢所有和任何帮助.

谢谢!

编辑:id喜欢看的样本输出

<pagenode title="home" url="~/" fornavbar="true">
 <pagenode title="events" url="~/admin/events" fornavbar="true"/>
 <pagenode title="catalog" url="~/catalog" fornavbar="true"/>
</pagenode>
Run Code Online (Sandbox Code Playgroud)

Nik*_*ohl 5

您可以使用这种方式迭代节点的属性,而xsl:foreach select="@*" 不必手动复制属性。如果您xsl:apply-templates 在 pagenode 元素内部调用,您应该会得到所需的结果。

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="pagenode[@fornavbar='true']">
        <pagenode>
            <xsl:for-each select="@*">
                <xsl:attribute name="{name(.)}"><xsl:value-of select="."/></xsl:attribute>
            </xsl:for-each>
            <xsl:apply-templates/>
        </pagenode>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

使

<?xml version="1.0"?>
<pagenode title="home" url="~/" fornavbar="true">
    <pagenode title="events" url="~/admin/events" fornavbar="true"/>
  <pagenode title="catalog" url="~/catalog" fornavbar="true"/>
</pagenode>
Run Code Online (Sandbox Code Playgroud)