michelc Blog

Just <strong>another</strong> WordPress.com weblog

Archive for the ‘xml’ Category

Throwing from XSLT

leave a comment »

Vu sur http://weblogs.asp.net/george_v_reilly/archive/2006/03/01/439402.aspx

I (George V. Reilly, not me) needed to add some declarative error checking to some XSLT templates recently. Specifically, I wanted to throw an error if my selects yielded an empty string, indicating that the input XML was wrong.

Unfortunately, there seems to be no easy way of doing this in XSLT, nor in XslTransform. The approved way is to validate against an XSD schema, but for various reasons, I didn’t want to go to the hassle of creating one.

I found a partial solution using xsl:message with the terminate= »yes » attribute. Under XslTransform.Transform() the following code throws an exception if the XPath expression is empty.

<xsl:if test="not(/some/xpath/expression)">
   <xsl:message terminate="yes">Missing expression</xsl:message>
</xsl:if>
<xsl:value-of select="/some/xpath/expression" />

It doesn’t do anything, however, in XMLSpy.

The downside, of course, is that you have to maintain the expression in two places, and the template becomes littered with those annoying tests.

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/1613)

Written by michel

2 mars 2006 at 7:27

Publié dans Code Snippets, xml

Blogmarks to Html conversion

with one comment

Ce fichier permet de transformer le fil atom de Blogmarks (par exemple http://api.blogmarks.net/user/ms_michel) en code html afin de pouvoir l’intégrer à une page web.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:atom="http://purl.org/atom/ns#draft-ietf-atompub-format-05" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <xsl:output method="html" />
  <xsl:template match="/">
    <xsl:apply-templates select="/atom:feed/atom:head" mode="before" />
    <xsl:apply-templates select="/atom:feed/atom:entry" />
    <xsl:apply-templates select="/atom:feed/atom:head" mode="after" />
  </xsl:template>
  <xsl:template match="atom:feed/atom:head" mode="before" >
    <!-- <h3><xsl:value-of select="atom:title" /></h3> -->
  </xsl:template>
  <xsl:template match="atom:feed/atom:head" mode="after">
    <p><a href="{atom:link[@rel='alternate']/@href}"><img src="http://blogmarks.net/img/88x31_neg.png" alt="blogmarks.net" /></a></p>
  </xsl:template>
  <xsl:template match="atom:feed/atom:entry">
    <div>
      <xsl:choose>
        <xsl:when test="position() mod 2 = 1">
          <xsl:attribute name="class">bm_blogmarks bm_odd</xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
          <xsl:attribute name="class">bm_blogmarks bm_even</xsl:attribute>
        </xsl:otherwise>
      </xsl:choose>
      <a href="{atom:link[@rel='related']/@href}"><img src="{atom:link[@rel='image']/@href}" alt="" /></a>
      <h4><a href="{atom:link[@rel='related']/@href}"><xsl:value-of select="atom:title" /></a></h4>
      <p><xsl:value-of select="atom:summary" disable-output-escaping="yes" /></p>
      <p class="blogmarks-tags">
        <xsl:value-of select="substring(atom:published, 0, 11)" />
        <xsl:if test="atom:category">
          <xsl:for-each select="atom:category">
            <xsl:text> - </xsl:text><a href="{@term}{@sheme}"><xsl:value-of select="@label" /></a>
          </xsl:for-each>
        </xsl:if>
      </p>
    </div>
  </xsl:template>
</xsl:stylesheet>

Le code html généré est inspiré par celui de Hot Links. Il peut ensuite être présenté grâce à la feuille de style CSS suivante :

.bm_blogmarks {
  margin: 10px auto;
  padding: 1%;
  background-color: #f5f5f5;
  border: 1px solid #d9d9d9;
  width: 97%;
  overflow:auto;
}
.bm_even {
  background-color: #fcfcfc;
}
.bm_blogmarks h4 {
  margin-top: 0;
}
.bm_blogmarks p.bm_tags {
  margin-bottom: 0;
  display: block;
  clear: left;
}
.bm_blogmarks img {
  margin: 0 0px 5px 10px;
  float: right;
  border: 0;
  clear: none;
  width: 144px;
  height: 107px;
}
.bm_even img {
  margin: 0 10px 5px 0px;
  float: left;
}

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/1206)

Written by michel

19 janvier 2006 at 8:18

Publié dans Code Snippets, html, xml

RDF to Html conversion

with one comment

Ce fichier permet de transformer un fil RDF en code html afin de pouvoir l’intégrer à une page web.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:foo="http://purl.org/rss/1.0/">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <xsl:apply-templates select="/rdf:RDF/foo:channel"/>
    </xsl:template>
    <xsl:template match="/rdf:RDF/foo:channel">
        <h3><xsl:value-of select="foo:title"/></h3>
        <p><xsl:value-of select="foo:description"/></p>
        <ul>
            <xsl:apply-templates select="/rdf:RDF/foo:item"/>
        </ul>
    </xsl:template>
    <xsl:template match="/rdf:RDF/foo:item">
        <li>
            <a href="{foo:link}" title="{substring(dc:date, 0, 11)}"><xsl:value-of select="foo:title"/></a>
            <p><xsl:value-of select="foo:description" disable-output-escaping="yes" /></p>
        </li>
    </xsl:template>
</xsl:stylesheet>

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/1164)

Written by michel

16 janvier 2006 at 3:04

Publié dans Code Snippets, html, xml

RSS to Html conversion

leave a comment »

Ce fichier permet de transformer un fil RSS (version 0.9x ou 2.0) en code html afin de pouvoir l’intégrer à une page web.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:wfw="http://wellformedweb.org/CommentAPI/">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <xsl:apply-templates select="/rss/channel"/>
    </xsl:template>
    <xsl:template match="/rss/channel">
        <h3><xsl:value-of select="title"/></h3>
        <p><xsl:value-of select="description"/></p>
        <ul>
            <xsl:apply-templates select="item"/>
        </ul>
    </xsl:template>
    <xsl:template match="/rss/channel/item">
        <li>
            <a href="{link}" title="{substring(pubDate, 0, 11)}"><xsl:value-of select="title"/></a>
            <p><xsl:value-of select="description" disable-output-escaping="yes" /></p>
        </li>
    </xsl:template>
</xsl:stylesheet>

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/1163)

Written by michel

16 janvier 2006 at 3:03

Publié dans Code Snippets, html, xml

Atom to Html conversion

with one comment

Ce fichier permet de transformer un fil atom en code html afin de pouvoir l’intégrer à une page web.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:atom="http://purl.org/atom/ns#"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:dc="http://purl.org/dc/elements/1.1/">
    <xsl:output method="html"/>
    <xsl:template match="/">
    <xsl:apply-templates select="/atom:feed/atom:head"/>
        <xsl:apply-templates select="/atom:feed"/>
    </xsl:template>
    <xsl:template match="atom:feed/atom:head">
        <h3><xsl:value-of select="atom:title"/></h3>
        <xsl:if test="atom:tagline"><p><xsl:value-of select="atom:tagline"/></p></xsl:if>
        <xsl:if test="atom:subtitle"><p><xsl:value-of select="atom:subtitle"/></p></xsl:if>
    </xsl:template>
    <xsl:template match="/atom:feed">
        <h3><xsl:value-of select="atom:title"/></h3>
        <xsl:if test="atom:tagline"><p><xsl:value-of select="atom:tagline"/></p></xsl:if>
        <xsl:if test="atom:subtitle"><p><xsl:value-of select="atom:subtitle"/></p></xsl:if>
        <ul>
            <xsl:apply-templates select="atom:entry"/>
        </ul>
    </xsl:template>
    <xsl:template match="atom:entry">
        <li>
            <a href="{atom:link[@rel='related']/@href}" title="{substring(atom:published, 0, 11)}"><xsl:value-of select="atom:title"/></a>
            <xsl:choose>
                <xsl:when test="atom:content != ''">
                    <p><xsl:value-of select="atom:content" disable-output-escaping="yes" /></p>
                </xsl:when>
                <xsl:otherwise>
                    <p><xsl:value-of select="atom:summary" disable-output-escaping="yes" /></p>
                </xsl:otherwise>
            </xsl:choose>
        </li>
    </xsl:template>
</xsl:stylesheet>

(Publié à l’origine sur http://www.bigbold.com/snippets/posts/show/1162)

Written by michel

16 janvier 2006 at 2:59

Publié dans Code Snippets, html, xml