xsl-list
[Top] [All Lists]

RE: [xsl] Is this the best way to emit one line per occurrence of an xpath?

2007-11-08 15:27:08
<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform";>

  <output method="text"/>

  <template match="/">
    <for-each select="/beans/bean[(_at_)singleton!='false']/@class">
      <value-of select="concat(normalize-space(), '&#10;')"/>
    </for-each>
  </template>

</stylesheet>


Unless you want the same behavior for @class in another section of your
stylesheet, I'd replace the template with a for-each, especially when
its content is so simple. <output method="text"/> is much simpler,
unless you're actually producing XML elsewhere in your stylesheet. And
with <output method="text"/>, you're not producing namespaces in your
output, so you can drop the xsl: prefix and work directly with the XSLT
elements.

But the logic is the same; all of the changes here are personal coding
style preferences. Take them or leave them, you asked. ^_^

~ Scott


-----Original Message-----
From: Karr, David [mailto:david(_dot_)karr(_at_)wamu(_dot_)net] 
Sent: Thursday, November 08, 2007 4:13 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Is this the best way to emit one line per occurrence of
an xpath?

I had to write a simple stylesheet that prints out in text form one line
of text for every occurrence of a particular xpath in an input xml file.
I think I got it working, but I just wanted to ask for a critique of
this, to see if there are different ways of doing this.

The following is what I have so far:
--------------------------
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

  <xsl:output method="xml" omit-xml-declaration="yes"/>
  
  <xsl:template match="/">
    <xsl:apply-templates
select="/beans/bean[(_at_)singleton!='false']/@class"/>
  </xsl:template>

  <xsl:template match="@class">
    <xsl:value-of select="normalize-space(.)"/><xsl:text>
</xsl:text>
  </xsl:template>

</xsl:stylesheet>
--------------------------

This produces output like this:

--------------------------
org.apache.commons.chain.impl.ChainBase
com.wamu.stuff.Gork
com.wamu.foo.Bar
com.wamu.uia.framework.AdapterController
com.wamu.uia.adapter.HttpClientAdapter
--------------------------

Again, this is exactly what I want, I was just wondering about
alternatives for doing the same thing.

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


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