[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Selectively convert Attributes to child Elements


Subject: Re: [xsl] Selectively convert Attributes to child Elements
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 18 May 2009 20:45:38 -0400

At 2009-05-18 14:18 -0700, The Web Maestro wrote:
The current INPUT XSL file (which I found & tweaked from
http://www.biglist.com/lists/lists.mulberrytech.com/xsl-list/archives/200011/msg00567.html)
doesn't output the ROOT element,

Because you are matching only on elements that have element parents, which is not true for the document element.


and currently strips all attributes
(it only outputs the parent element if it strips the attribute). With
the exception of the above attributes being output as child elements,
I want to preserve the current XML document.

Then you need to push the child nodes of the elements through the template rules as well ... you are missing <xsl:apply-templates/> in your template rule.


But it isn't that simple. You cannot convert some attributes to elements and then after that add the next attributes that are preserved as attributes.

Below illustrates how one could address your requirement. A quick review of the result looks like it is what you want.

I hope this helps.

. . . . . . . . Ken

T:\ftemp>type maestro.xml
<?xml version="1.0" encoding="UTF-8"?>
<buckets siteid="mysiteid" version="1.0">
  <bucket priority="4" bucketid="12348" title="Sports"
displaytitle="Sports" link="/sections/sports/" target="_blank"
display="1">
    <nav>
      <item priority="1" title="Baseball" displaytitle="Baseball"
link="/sections/sports/baseball/" description="Baseball 411"
display="1"/>
      <item priority="2" title="Football" displaytitle="Football"
link="/sections/sports/football/" description="" display="1"/>
    </nav>
    <contentInclude priority="1" max="3" display="1">
      <item catid="2345" catname="Sports" prefix="" display="1"/>
    </contentInclude>
    <contentInclude type="daytime" priority="2" max="2" display="1">
      <item catname="sports" display="1"/>
    </contentInclude>
    <contentInclude type="blog" priority="3" max="2" display="1">
      <item catname="coolblog" prefix="Cool: " display="1"/>
      <item catname="soundblog" prefix="Sounds: " display="1"/>
    </contentInclude>
    <bucketBottomLink title="More Sports &#187;"
link="/sections/entertainment/" display="1"/>
  </bucket>
  <bucket priority="3" bucketid="12347" title="Promotions"
displaytitle="" description="This is the Promotions bucket" link=""
display="1" tags="promos,promotions,nonpublic">
    <contentInclude priority="1" display="1">
      <item catid="5678" catname="Promos" prefix="" display="1"/>
    </contentInclude>
  </bucket>
  <bucketLists>
    <bucketList priority="4" bucketListid="4" location="homepage"
title="Entertainment List" displaytitle="Entertainment Junkie"
display="1" tags="entertainment,fun">
      <bucketListItem priority="1" bucketid="34572"
title="Entertainment" display="1"/>
      <bucketListItem priority="5" bucketid="34576" type="blog"
title="artsblog" items="7" display="1"/>
    </bucketList>
    <bucketList priority="5" bucketListid="5" location="homepage"
title="Nerd" displaytitle="Nerd's List of Lists" display="1"
tags="tech,technology,gadgets,nerd,geek">
      <bucketListItem priority="1" bucketid="34577" title="Science
&amp; Technology" display="1"/>
      <bucketListItem priority="4" bucketid="34582" type="blog"
title="nerdblog" items="7" display="1"/>
    </bucketList>
  </bucketLists>
</buckets>

T:\ftemp>call xslt maestro.xml maestro.xsl
<?xml version="1.0" encoding="utf-8"?>
<buckets siteid="mysiteid" version="1.0">
<bucket priority="4" bucketid="12348" link="/sections/sports/" target="_blank" display="1">
<nav>
<item priority="1" link="/sections/sports/baseball/" display="1"/>
<item priority="2" link="/sections/sports/football/" display="1"/>
</nav>
<contentInclude priority="1" max="3" display="1">
<item catid="2345" display="1"/>
</contentInclude>
<contentInclude type="daytime" priority="2" max="2" display="1">
<item display="1"/>
</contentInclude>
<contentInclude type="blog" priority="3" max="2" display="1">
<item display="1"/>
<item display="1"/>
</contentInclude>
<bucketBottomLink link="/sections/entertainment/" display="1"/>
</bucket>
<bucket priority="3" bucketid="12347" link="" display="1" tags="promos,promotions,nonpublic">
<contentInclude priority="1" display="1">
<item catid="5678" display="1"/>
</contentInclude>
</bucket>
<bucketLists>
<bucketList priority="4" bucketListid="4" location="homepage" display="1" tags="entertainment,fun">
<bucketListItem priority="1" bucketid="34572" display="1"/>
<bucketListItem priority="5" bucketid="34576" type="blog" items="7" display="1"/>
</bucketList>
<bucketList priority="5" bucketListid="5" location="homepage" display="1" tags="tech,technology,gadgets,nerd,geek">
<bucketListItem priority="1" bucketid="34577" display="1"/>
<bucketListItem priority="4" bucketid="34582" type="blog" items="7" display="1"/>
</bucketList>
</bucketLists>
</buckets>
T:\ftemp>type maestro.xsl
<!-- AttrToElement.xsl: Turn all attributes into subelements -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>


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

<!--handle attributes at the time they are being preserved-->
<xsl:template mode="preserve"
              match="@title|@displaytitle|@description|
                     @catname|@prefix|@text"/>

<xsl:template mode="preserve" match="@*">
  <xsl:copy/>
</xsl:template>

<!--handle attributes at the time they are being converted-->
<xsl:template match="@title|@displaytitle|@description|
                     @catname|@prefix|@text">
  <xsl:for-each select="@*">
    <xsl:element name="{name()}">
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:for-each>
</xsl:template>

<xsl:template match="@*"/>

</xsl:stylesheet>

T:\ftemp>rem Done!


-- XSLT/XSL-FO/XQuery hands-on training - Los Angeles, USA 2009-06-08 Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18 Video overview: http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18 G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal


Current Thread
Keywords