xsl-list
[Top] [All Lists]

[xsl] Selectively convert Attributes to child Elements

2009-05-18 17:18:48
NOTE: I had to repost from a separate email address. My apologies if
you are seeing this again.

I want to transform an XML document, and selectively convert
attributes to child elements (some but not all attributes).

These are the attributes I'm trapping:

<xsl:for-each select="@title|@displaytitle|@description|@catname|@prefix|@text">

PROBLEMS:
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, 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.

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

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:for-each
select="@title|@displaytitle|@description|@catname|@prefix|@text"> -->
        <xsl:for-each select="@*">
            <xsl:element name="{name()}"><xsl:value-of
select="."/></xsl:element>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

DESIRED OUTPUT XML:
<?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">
      <title>Sports</title>
      <displaytitle>Sports</displaytitle>
    <nav>
      <item priority="1" link="/sections/sports/baseball/" display="1">
          <title>Baseball</title>
          <displaytitle>Baseball</displaytitle>
          <description>Baseball 411</description>
      </item>
      <item priority="2" link="/sections/sports/football/" display="1">
          <title>Football</title>
          <displaytitle>Football</displaytitle>
      </item>
    </nav>
    <contentInclude priority="1" max="3" display="1">
      <item catid="2345" prefix="" display="1">
          <catname>Sports</catname>
      </item>
    </contentInclude>
    <contentInclude type="daytime" priority="2" max="2" display="1">
      <item display="1">
          <catname>sports</catname>
      </item>
    </contentInclude>
    <contentInclude type="blog" priority="3" max="2" display="1">
      <item display="1">
          <catname>coolblog</catname>
          <prefix>Cool: </prefix>
      </item>
      <item display="1">
          <catname>soundblog</catname>
          <prefix>Sounds: </prefix>
      </item>
    </contentInclude>
    <bucketBottomLink link="/sections/entertainment/" display="1">
        <title>More Sports &#187;</title>
    </bucketBottomLink>
  </bucket>
  <bucket priority="3" bucketid="12347" link="" display="1"
tags="promos,promotions,nonpublic">
      <title>Promotions</title>
      <description>This is the Promotions bucket</description>
    <contentInclude priority="1" display="1">
      <item catid="5678" display="1">
          <catname>Promos</catname>
      </item>
    </contentInclude>
  </bucket>
  <bucketLists>
    <bucketList priority="4" bucketListid="4" location="homepage"
display="1" tags="entertainment,fun">
        <title>Entertainment List</title>
        <displaytitle>Entertainment Junkie</displaytitle>
      <bucketListItem priority="1" bucketid="34572" display="1">
          <title>Entertainment</title>
      </bucketListItem>
      <bucketListItem priority="5" bucketid="34576" type="blog"
items="7" display="1">
          <title>coolblog</title>
      </bucketListItem>
    </bucketList>
    <bucketList priority="5" bucketListid="5" location="homepage"
display="1" tags="tech,technology,gadgets,nerd,geek">
        <title>Nerd</title>
        <displaytitle>Nerd's List of Lists</displaytitle>
      <bucketListItem priority="1" bucketid="34577" display="1">
          <title>Science &amp; Technology</title>
      </bucketListItem>
      <bucketListItem priority="4" bucketid="34582" type="blog"
items="7" display="1">
          <title>nerdblog</title>
      </bucketListItem>
    </bucketList>
  </bucketLists>
</buckets>

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>