xsl-list
[Top] [All Lists]

RE: Design question

2003-02-11 13:12:21
It may have been 7 mos. old, but it was helpful for me to
see the question and your answer.  I'm even more of a beginner
than you so it was a good exercise for me too.

However, a couple of things are missing from your solution...
(1) the da: namespace prefix, and
(2) capitalizing 'Positive' element name.

Below is my solution (based on yours).

However when I tried to add "da:" to the output elements,
xsl gave me an unknown namespace error unless I added
a namespace declaration to the <xsl:stylesheet> element.  Whereupon
the output da:Positive element also took on a xmlns:da="..." declaration.
I assume that's by design though I haven't thought through
why it ought to be so.

Anyway, I welcome anyone's comments on how to do this better.

Lars


<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
  xmlns:da='something'>
<xsl:output method="xml" indent="yes"/>

<!--
 Input:
    <test type="positive" name="Test 1">
        <param name="p1">123</param>
        <param name="date1">July 9</param>
        <param name="p2">false</param>
    </test>

 Desired output:
    <da:Positive name="Test 1" p1="123" p2="false">
       <da:Date number="1" value="July 9"/>
    </da:Positive>
-->

<xsl:template match="/">
  <xsl:apply-templates select="test"/>
</xsl:template>

<xsl:template match="test">
  <xsl:element name="{concat('da:', translate(substring(@type, 1, 1),
'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
substring(@type, 2))}">
    <xsl:copy-of select="@name"/>
    <xsl:apply-templates select="param[contains(@name,'p')]"/>
    <xsl:apply-templates select="param[contains(@name,'date')]"/>
  </xsl:element>
</xsl:template>

<xsl:template match="param[contains(@name,'p')]">
  <xsl:attribute name="{(_at_)name}">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>

<xsl:template match="param[contains(@name,'date')]">
  <xsl:element name="da:Date">
    <xsl:attribute name="number">
      <xsl:value-of select="translate(@name,

'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz','')"/>
    </xsl:attribute>

    <xsl:attribute name="value">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:element>
</xsl:template>
</xsl:stylesheet>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>