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

Re: [xsl] transforming MRSS


Subject: Re: [xsl] transforming MRSS
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Mon, 08 Aug 2011 18:40:24 +0200

Hi Dirk,

Forgive me for ignoring your most recent post, as I don't think it's fair towards the people that try to help you. However, I do believe that your original question deserves a slightly deeper coverage. Maybe you already unsubscribed, but then I'll write this to any future readers of the XSL archives.

A "complete" example of working with XML tags that have a colon in their name:

You refer to MRSS. That's an open standard and defines a namespace called "http://search.yahoo.com/mrss/". It's not mandatory by the specification to bind this to the prefix "media". As a matter of fact, there are very few namespaces that must be bound to a particular prefix. Just like MRSS, XSLT is also an open standard that defines its namespace a similar way.

Result:
- if you want to use MRSS you must use and understand namespaces
- if you want to use XSLT you must use and understand namespaces.

Whether you blame XSLT for having namespaces is equal to blaming MRSS for having namespaces, or XML for that matter, which is the mother language of all these languages: MRSS, XSLT, but also XHTML, SVG, RSS are all specifief in XML and namespaces. If you want to do any serious programming with XML based languages like the above, then you have to deal with namespaces. If you don't like it, that's fine. I love C#, but I don't like the way it works with namespaces (not related). I like PHP, but I can't stand the way it uses global variables.

Every language will have a thing or two that you won't like. But when you look deeper, you start to understand why it is designed the way it is and it becomes easier to accept it. Sometimes this takes hours, sometimes years. Depends on how keen you are on adopting a new language.

Now, after all this babble you still don't have an example on how to deal with namespaces, you only learned that whatever step you take, will always involve namespaces, whether you do it with PHP, with XSLT, in Ruby or all of the above. I can help you with the XSLT part. It's trivial:

<xsl:stylesheet version="1.0"
    xmlns:media="http://search.yahoo.com/mrss/"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" indent="true"/>

<!-- catch the content tags of MRSS -->
<xsl:template match="media:content">
<div><xsl:value-of select="." /></div>
</xsl:template>

<!-- catch everything else and ignore it, but keep processing children -->
<xsl:template match="*|@*">
<xsl:apply-templates select="*"/>
</xsl:template>
</xsl:stylesheet>

Simple, isn't it? Just a few lines and it outputs every content element from MRSS as a <div> element. You see that in only a few lines of code you can do a relatively complex transformation which will require a lot more work if you do it by hand in PHP or C#. But that's the beauty of XSLT: it's simple and elegant and, while it requires a bit of challenge in the beginning, can save you tons of work once you get the hang of it.

But that's the same as with any language: only once you mastered it, the real fun begins (but while that can take years with traditional general purpose languages like PHP, Ruby, C, C#, COBOL etc, it typically takes one to two weeks for someone to become relatively proficient in XSLT).


why does XSL have to be more complicated than (e.g.)
simplexml_load_file() from PHP?
With all this crazy, over-complicated namespace junk... (i mean
xmlns:yaddayadda) that
is broken or wrong in many feeds anyways...

You compare a whole language with a single function in another language? You can't compare a car to fin of a dolphin, but both can be used to travel. The fin provides limited yet rudimentary way to travel, the car has slightly more features. The same with XSLT and that function you mentioned. XSLT is a full-blown language, in use since roughly 13 years and used in almost all situations where XML needs to be transformed. simplexml_load_file can only do a few things and is only available inside PHP.


About the crazy over-complicated namespace junk you've received plenty of answers. But please keep in mind that MRSS itself also has this "namespace junk" around, as does any language built on XML. Consider the prefixes a placeholder for a namespace and consider the namespace a library in a language. That covers most of it and with that mindset and 10 minutes to think it through, it should go from "over-complicated" to "complicated" to "interesting" to "easy" to "trivial". If it's broken in many feeds, then fix it.

 why not just access a tag named <media:group>
 with "media:group"?

See above in my example. It's as simple as that. But if your input is broken, it's not valid MRSS and it's not valid XML. If it's not valid XML, it cannot be processed with XSLT 1.0. The trick is, just as you may have been used to do with HTML, to tidy up the MRSS before it is processed. With HTML there's HTML-Tidy. I believe there's something similar for XML. But your best bet is to fix it at the source. If they don't know how to deal with a standard, they shouldn't use it or propagate it as MRSS, which it isn't.


My 2 cents.

Kind regards and best of luck,

Abel Braaksma



------------------------------------------------------------------------
From: 	Dirk <noisyb@xxxxxxx>
Sent: 	Monday, August 08, 2011 3:25:40 AM
To: 	xsl-list
Cc: 	
Subject: 	[xsl] transforming MRSS



Hello,

I fail to find a working example for transforming XML where tags have a colon in their name... it's always only fragments of a solution which, of course, do not work...

1) is there any *complete* example that shows how to do it? preferably for MRSS...

2) why does XSL have to be more complicated than (e.g.) simplexml_load_file() from PHP? With all this crazy, over-complicated namespace junk... (i mean xmlns:yaddayadda) that is broken or wrong in many feeds anyways... why not just access a tag named <media:group> with "media:group"?


Dirk


Current Thread
Keywords