[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: convert nested elements, ID's and classes to CSS

Subject: Re: convert nested elements, ID's and classes to CSS declaration
From: xslt user <xsltacct@xxxxxxxxx>
Date: Wed, 21 Mar 2007 13:08:45 -0700 (PDT)
Re:  convert nested elements
Thanks! I was working on this in the background before
I got a chance to read the replies and came up with
pretty much the exact same solution... :)  I added in
ignoring text nodes also.

<?xml version="1.0" encoding="utf-8"
standalone="yes"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0"
encoding="utf-8" omit-xml-declaration="yes"
standalone="no" indent="no" media-type="text/xml"/>

<xsl:template match="/">
/*---  ---*/
<xsl:apply-templates/>
/*---  ---*/
</xsl:template>

<xsl:template match="node()">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="name(.)"/><xsl:text> </xsl:text>
<xsl:if test="@id">#<xsl:value-of
select="@id"/><xsl:text> </xsl:text></xsl:if>
<xsl:if test="@class">.<xsl:value-of
select="@class"/><xsl:text> </xsl:text></xsl:if>
</xsl:for-each>
<xsl:text>{  }
</xsl:text>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()">
<xsl:apply-templates/>
</xsl:template>

</xsl:transform>



I'll have to take a closer look at Michael Kay's
response too.  I'm using xslt 1.0 for now but maybe
the &#xa is better?



XSLT 1.0 or 2.0? Here's a 2.0 solution, untested:

<xsl:template match="*">
 <xsl:apply-templates
select="ancestor-or-self::*/(.|@class|@id)"
mode="token"/>
 <xsl:text>&#xa;</xsl:text>
</xsl:template>

<xsl:template match="*" mode="token">
  <xsl:value-of select="concat(name(), ' ')"/>
</xsl:template>

<xsl:template match="@class" mode="token">
  <xsl:value-of select="concat('.', ., ' ')"/>
</xsl:template>

<xsl:template match="@id" mode="token">
  <xsl:value-of select="concat('#', ., ' ')"/>
</xsl:template>

Michael Kay
http://www.saxonica.com/



--- Jay Bryant <jay@xxxxxxxxxxxx> wrote:

> > Hello,
> >
> > I searched for this all over and couldn't find a
> > solution.  I want to take the following XML:
> >
> > <root>
> > <file>
> > <element/>
> > </file>
> > <file id="a">
> > <element id="a"/>
> > </file>
> > <file class="b">
> > <element class="b"/>
> > </file>
> > <file id="c" class="d" not="1" this="2">
> > <element id="c" class="d" not="1" this="2"/>
> > </file>
> > </root>
> >
> >
> > #### and output something this ####
> >
> >
> > root
> > root file
> > root file element
> > root file #a
> > root file #a element #a
> > root file .b
> > root file .b element .b
> > root file #c .d
> > root file #c .d element #c .d
> >
> >
> > #### ignoring any attributes that are not "id" or
> > "class" and containing all ancestor elements and
> id's
> > and classes no matter what the element name is or
> how
> > many levels deep.  I'd like to use this for auto
> > generating CSS declarations from XHTML snipits, I
> can
> > deal with removing redundant rows manually.  Below
> is
> > the closest I've gotten which only strips the XML
> to
> > the elements and attributes I'm interested in, but
> > doesn't format it or list ancestor elements, id's
> and
> > classes (I'm a beginner at XSLT) thanks! ####
> >
> >
> > <xsl:template match="node()|@id|@class">
> > <xsl:copy>
> > <xsl:apply-templates select="node()|@id|@class"/>
> > </xsl:copy>
> > </xsl:template>
> 
> Here's one way:
> 
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0" 
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> 
> <xsl:output method="text" indent="no"/>
> 
> <xsl:template match="*">
> <xsl:for-each select="ancestor-or-self::*">
> <xsl:value-of select="name()"/><xsl:text>
> </xsl:text>
> <xsl:if test="@id">#<xsl:value-of
> select="@id"/><xsl:text> 
> </xsl:text></xsl:if>
> <xsl:if test="@class">.<xsl:value-of
> select="@class"/><xsl:text> 
> </xsl:text></xsl:if>
> </xsl:for-each>
> <xsl:apply-templates/>
> </xsl:template>
> 
> </xsl:stylesheet>
> 
> Jay Bryant
> Bryant Communication Services 
> 
> 



 
____________________________________________________________________________________
Expecting? Get great news right away with email Auto-Check. 
Try the Yahoo! Mail Beta.
http://advision.webevents.yahoo.com/mailbeta/newmail_tools.html 

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2011 All Rights Reserved.