[olug] [OT] Parsing XML with command line XSLT xsltproc...

DYNATRON tech dynatron at gmail.com
Wed Feb 3 08:06:50 UTC 2010


you need an <xsl:for-each select="fieldyouarettestingfor"> statement to wrap
around like so....

<xsl:for-each select="rss/channel">
 ALBUM="<value-of select="title"/>"
 TITLE="<value-of select="item/title"/>"
 FILE="<value-of select="item/enclosure/@url"/>"
</xsl:for-each>

the for-each tag changes your working location in the xpath to the item your
are testing for, so be sure to mind what you are pointing at with select in
the value-of attribute.

below is an abridged version of my xslt (and input xml) for rss creation of
some blogging software i work on.
sounds like this is the opposite of what you are trying to do, but figured
it would make a handy example in your xslt adventures. xslt is very powerful
and i'd recommend that anyone who works with xml or html on a regular basis
uses it.
for processing, i use xalan in linux CLI and msxsl if i'm stuck with a
windows CLI.
with xalan it's easy to automate within a bash script and it has really good
--options.


______________________
<?xml version="1.0" encoding="utf-8"?>
 <!--
 S L A R S S   T R A N S F O R M  (abridged)
 Owner:         dynatron digital services
 Started:       2009-12-16-06-51
 Modified:      2010-02-03-02-00
 Version:       fork
 -->
<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" encoding="UTF-8"/>
 <xsl:param name="stylesheet"/>
 <xsl:template match="/">
   <rss version="2.0">
    <channel>
     <title><xsl:value-of select="site"/></title>
     <link>http://<xsl:value-of select="site"/></link>
     <description><xsl:value-of select="mission"/></description>
     <image>
      <url><xsl:value-of select="avatar"/></url>
      <title><xsl:value-of select="site"/></title>
      <link>http://<xsl:value-of select="site"/></link>
     </image>
     <xsl:for-each select="post">
      <item>
       <author><xsl:value-of select="../contact/name"/></author>
       <title><xsl:value-of select="postID"/> - <xsl:value-of
select="title"/></title>
       <link>./html/<xsl:value-of select="postID"/>.html</link>
       <guid>./html/<xsl:value-of select="postID"/>.html</guid>
       <description><xsl:value-of select="outline/thesis"/></description>
      </item>
     </xsl:for-each>
    </channel>
   </rss>
 </xsl:template>
</xsl:stylesheet>
_______________________________




using this input
_______________________________
<?xml version="1.0" encoding="utf-8"?>
  <site>slauce.com/log</site>
  <avatar>http://dynatron.org/imagearchive/avatar.jpg</avatar>
  <mission>droppin' logs</mission>
  <contact>
   <name>f.b.h.</name>
   <address>dynatron at gmail.com</address>
  </contact>
  <post>
   <postID>yyyyMMddhhmm</postID>
   <title>TitleofPost</title>
   <outline>
    <thesis>Thesistext</thesis>
   </outline>
  </post>




On Mon, Feb 1, 2010 at 10:30 PM, Dan Linder <dan at linder.org> wrote:

> I listen to quit a few podcasts, but some of their internal MP3
> title/album/etc fields messed up.  My podcatcher (bashpodder -
> http://lincgeek.org/bashpodder/) uses the xsltproc command line tool
> to parse the RSS feed for the URLs for the MP3s.
>
> When I try to parse an RSS feed with my XSL file (see below), I get
> only the first RSS entry field:
>    xsltproc parse.xsl http://leoville.tv/podcasts/sn.xml
>
> Any idea how to modify the XSL file to work on all RSS entries?
>
> Once I get that, it's fairly easy to use mp3info2 to fix the internal
> MP3 fields so my player shows usable information.
>
> On a side note, this site was a good primer on XSL:
>    * http://accu.org/index.php/journals/311
> Any others that people have found helpful?
>
> Thanks,
>
> Dan
>
> === begin parse.xsl ===
> <?xml version="1.0"?>
> <!--  -->
> <stylesheet version="1.0"
>        xmlns="http://www.w3.org/1999/XSL/Transform">
>  <output method="text"/>
>  <template match="/" >
> ###########################
> ALBUM="<value-of select="/rss/channel/title"/>"
> TITLE="<value-of select="/rss/channel/item/title"/>"
> FILE="<value-of select="/rss/channel/item/enclosure/@url"/>"
> ###########################
>  </template>
> </stylesheet>
> === end parse.xsl ===
>
> --
> ***************** ************* *********** ******* ***** *** **
> "Quis custodiet ipsos custodes?"
>    (Who can watch the watchmen?)
>    -- from the Satires of Juvenal
> "I do not fear computers, I fear the lack of them."
>    -- Isaac Asimov (Author)
> ** *** ***** ******* *********** ************* *****************
> _______________________________________________
> OLUG mailing list
> OLUG at olug.org
> https://lists.olug.org/mailman/listinfo/olug
>



-- 
dynatron digital services
box 191 - 68037
www.dynatron.org
dynatron at gmail.com



More information about the OLUG mailing list