...">

Xslt copy without children

Hi I have a sitemap XML document that looks something like this.

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

Now I want to get an XML document for the navigation bar that includes all pagenodes with fornavbar = true. How can I do that?

The closest I could get to this:

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

the problem with this is that it includes all the children of any that matches the navbar

I only want to copy all the attributes, not all the children

but if i try

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

then I have 2 problems

  • I can type each attribute separately, and I have quite a few pages and they can eventually change
  • he is losing his hierarchy. everything becomes flat one after another.

I would be grateful to everyone and any help in this matter.

Thank you!

EDIT: sample output on which id likes to see

<pagenode title="home" url="~/" fornavbar="true">
 <pagenode title="events" url="~/admin/events" fornavbar="true"/>
 <pagenode title="catalog" url="~/catalog" fornavbar="true"/>
</pagenode>
+5
3

node xsl:foreach select="@*" , . xsl:apply-templates yor 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>

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

, , XSLT:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="*[@fornavbar = 'false']">
  <xsl:apply-templates/>
 </xsl:template>
</xsl:stylesheet>

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

, :

<pagenode title="home" url="~/" fornavbar="true">
   <pagenode title="events" url="~/admin/events" fornavbar="true"/>
   <pagenode title="catalog" url="~/catalog" fornavbar="true"/>
</pagenode>

:

  • () node "as-is". - XSLT.

  • , - , fornavbar - "false". - .

+3

XSLT should look like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="pagenode[@fornavbar='true']">
    <pagenode>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </pagenode>
  </xsl:template>
</xsl:stylesheet>
+1
source

All Articles