attributes - xslt copy-of without children -
hi have sitemap xml document looks 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 want retrieve xml document navbar, includes pagenodes have fornavbar=true. how can done?
the closest able far 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 includes children of matched navbar
i want copy attributes, not children
but if 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 have 2 problems
- i might type out each attribute separately, , have quite few per page , theyre apt change eventually
- it loses hierarchy. becomes flat 1 after other
i appreciate , in matter.
thank you!
edit: sample output id see
<pagenode title="home" url="~/" fornavbar="true"> <pagenode title="events" url="~/admin/events" fornavbar="true"/> <pagenode title="catalog" url="~/catalog" fornavbar="true"/> </pagenode>
you can iterate on attributes of node using xsl:foreach select="@*"
way don't have copy attributes hand. if call xsl:apply-templates
inside of yor pagenode element should desired result.
<?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>
makes
<?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>
Comments
Post a Comment