Kolleyk
Kolleyk

Reputation: 23

Generic xml -> html conversion using xslt and convention?

I have been scouring the web for something like this and being a lazy programmer, before I attempt to do so myself, I thought I would ask here.

What I would like is a method to take any xml with node names following a convention (like cakephp or ruby) and then for that data to be presented in a ready to print format.

ie:

<xml>
<home_address>The Street</home_address>
</xml>

to:

<tr><td>Home Address</td><td>The Street</td></tr>

etc. with children having separate tables.

It seems pretty straightforward and something that would have been desired many times before. I found one useful discussion, but with no conclusion.

http://www.daniweb.com/software-development/xml-xslt-and-xpath/threads/363621/xml-to-html-using-xslt-without-hardcoding-node-names-in-xslt

Have I missed something here? Is there a simple generic xslt/css method for doing this? Or is this work being repeated hundreds of times a day in cubicles around the world?

Thanks in advance,

F

Upvotes: 0

Views: 1309

Answers (1)

james31rock
james31rock

Reputation: 2705

use the following XSL stylesheet. I have tested it and it works.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:template match="/">
    <xsl:for-each select="*">
      <xsl:for-each select="*">
        <tr>
          <xsl:variable name="NodeNameClearText">
            <xsl:call-template name="repalceNodeName">
              <xsl:with-param name="value" select="local-name(.)"/>
            </xsl:call-template>
          </xsl:variable>
              <td>
                <xsl:value-of select="$NodeNameClearText" />
              </td>
              <td>
                <xsl:value-of select="." />
              </td>
            </tr>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="repalceNodeName">
    <xsl:param name="value"/>
    <xsl:variable name="valueWithoutUnderscores">
      <xsl:value-of select="translate($value, '_',' ')"/>
    </xsl:variable>
    <xsl:call-template name="caseLowerAcceptFirstWord">
      <xsl:with-param name="data" select="$valueWithoutUnderscores"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="caseDown">
    <xsl:param name="data"/>
    <xsl:if test="$data">
      <xsl:choose>
        <xsl:when test="starts-with($data,' ')">
          <xsl:text> </xsl:text>
          <xsl:call-template name="caseLowerAcceptFirstWord">
            <xsl:with-param name="data" select="normalize-space(substring($data,2))"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="translate(substring($data,1,1),
                                  'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"/>
          <!-- put all the chars you want to change 
                 into the last two strings -->
          <xsl:call-template name="caseDown">
            <xsl:with-param name="data" select="substring($data,2)"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>

  <xsl:template name="caseUP">
    <xsl:param name="data"/>
    <xsl:if test="$data">
      <xsl:choose>
        <xsl:when test="starts-with($data,' ')">
          <xsl:text> </xsl:text>
          <xsl:call-template name="caseLowerAcceptFirstWord">
            <xsl:with-param name="data" select="substring($data,2)"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="translate(substring($data,1,1),
                                  'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
          <!-- put all the chars you want to change 
                 into the last two strings -->
          <xsl:call-template name="caseDown">
            <xsl:with-param name="data" select="substring($data,2)"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>

  <xsl:template name="caseLowerAcceptFirstWord">
    <xsl:param name="data"/>
    <xsl:variable name="upperData">
      <xsl:call-template name="caseUP">
        <xsl:with-param name="data" select="$data"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:if test="$upperData">
      <xsl:value-of select="substring($upperData,1,1)"/>
      <xsl:call-template name="caseDown">
        <xsl:with-param name="data" select="substring($data,2)"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

A little more info on what I tested. The following XML:

<xml>
<home_address>The Street</home_address>
<po_box>474</po_box>
</xml>

Was output to

<tr><td>Home Address</td><td>The Street</td></tr>
<tr><td>Po Box</td><td>474</td></tr>

If thats not what you wanted your question is to vague

Upvotes: 1

Related Questions