XML to HTML with XSL, using attributes?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Digital Puer

    #1

    XML to HTML with XSL, using attributes?

    I am trying to use XSL to parse XML into HTML, but
    I'm having a hard time with parsing XML attributes.

    My XML looks like this:

    <td> data </td>
    <td width="10"> data </td>

    I want the resulting HTML to look exactly like the above!

    My XSL looks like this:
    ----
    <xsl:template match="td">
    <td>
    <xsl:attribut e name="width">
    <xsl:value-of select="@width"/>
    </xsl:attribute>
    <xsl:apply-templates/>
    </td>
    </xsl:template>
    ----

    However, this produces:

    <td width=""> data </td>
    <td width="10"> data </td>


    Can someone answer the following questions:
    1. How can the 'width=""' be removed if there is no 'width' attribute'?

    2. can someone comment if the above template is the right way to go
    to process XML attributes, in general? I'm a newbie. thanks.
  • Dimitre Novatchev

    #2
    Re: XML to HTML with XSL, using attributes?

    > Nope, After much staring, I can't work it out! What is this doing?[color=blue]
    >
    > It appears to match any attribute or node() - or possibly any element with
    > an attribute or node()? I'm not sure.
    >
    > It then shallow copies whatever it's matched (suggesting the match does
    > actually match elements), then applies templates to, er, whatever it
    > matches itself. That doesn't make sense, even to me, and I wrote it!
    >
    > I give up! Can you explain this in newbie terms?[/color]

    In such case the best advice is to read a good XSLT book -- e.g. Jeni
    Tenisson's "Beginning XSLT".

    Read also: http://www.w3.org/TR/xslt#copying

    This template copies every node and its attributes, then applies itself
    (note that we don't have any other competing template) on the children of
    this node -- and so on recursively, until no node matched has any children.
    Thus the complete tree starting from the root node to the leaves is copied.


    =====
    Cheers,

    Dimitre Novatchev.
    http://fxsl.sourceforge.net/ -- the home of FXSL


    Comment

    • Stuart A Yeates

      #3
      Re: XML to HTML with XSL, using attributes?

      Derek Fountain wrote:[color=blue]
      > Dimitre Novatchev wrote:[color=green]
      >>As you're re-creating exactly the source xml document, it's much simpler
      >>to use the identity template:
      >>
      >><xsl:styleshe et version="1.0"
      >> xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
      >>
      >> <xsl:output omit-xml-declaration="ye s"/>
      >>
      >> <xsl:template match="@* | node()">
      >> <xsl:copy>
      >> <xsl:apply-templates select="@* | node()"/>
      >> </xsl:copy>
      >> </xsl:template>
      >>
      >></xsl:stylesheet>[/color]
      >
      > It appears to match any attribute or node() - or possibly any element with
      > an attribute or node()? I'm not sure.
      >
      > It then shallow copies whatever it's matched (suggesting the match does
      > actually match elements), then applies templates to, er, whatever it
      > matches itself. That doesn't make sense, even to me, and I wrote it!
      >
      > I give up! Can you explain this in newbie terms?[/color]

      You're right, it matches attribute or node then it makes a shallow copy
      of the attribute or node and recursively calls itself on all
      attributes or nodes in the current context (i.e. any attributes or nodes
      that are children original matched attribute or node).

      stuart

      Comment

      • Derek Fountain

        #4
        Re: XML to HTML with XSL, using attributes?

        >>><xsl:stylesh eet version="1.0"[color=blue][color=green][color=darkred]
        >>> xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
        >>>
        >>> <xsl:output omit-xml-declaration="ye s"/>
        >>>
        >>> <xsl:template match="@* | node()">
        >>> <xsl:copy>
        >>> <xsl:apply-templates select="@* | node()"/>
        >>> </xsl:copy>
        >>> </xsl:template>
        >>>
        >>></xsl:stylesheet>[/color][/color]
        > You're right, it matches attribute or node then it makes a shallow copy
        > of the attribute or node and recursively calls itself on all
        > attributes or nodes in the current context (i.e. any attributes or nodes
        > that are children original matched attribute or node).[/color]

        OK, a bit of experimentation with an XPath evaluator shows what it matches,
        and I can understand what is going on. :o)

        Why use this instead of <xsl:copy-of>? That does a deep copy of an element,
        including all its attributes and nodes, doesn't it?

        Comment

        • Derek Fountain

          #5
          Re: XML to HTML with XSL, using attributes?

          > Because one can override this very general tempalate with templates[color=blue]
          > specific just for a given element or attribute and thus solve such tasks[/color]

          Ah, yes, OK. That makes sense.
          [color=blue]
          > Once again, this is basic stuff that is best studied with a good book.
          > Asking isolated questions in a newsgroup will not provide the systematic
          > knowledge that one will acquire from such a book just in one month's time.[/color]

          I'm doing both. I've been working through O'Reilly's XSLT book for a few
          days now, and, despite the typos, it's teaching me a lot. However, real
          world examples, like those found in this NG, add a depth and challenge to
          understanding that you just can't get from a book.

          For me, taking a newbie's question from here and working with the book, the
          XSLT/XPath specs and some test code, to find out the answer for myself
          produces a much better understanding than just working through the book. Of
          course, it does mean that if I can't work it out, and I don't understand
          the answers posted in the NG, I have to ask for line by line clarification.
          I suppose that could annoy some people - I must sound like a little kid
          going "why? why? whhhhhhy?" :o)

          Comment

          • Dimitre Novatchev

            #6
            Re: XML to HTML with XSL, using attributes?

            > > Once again, this is basic stuff that is best studied with a good book.[color=blue][color=green]
            > > Asking isolated questions in a newsgroup will not provide the systematic
            > > knowledge that one will acquire from such a book just in one month's[/color][/color]
            time.[color=blue]
            >
            > I'm doing both. I've been working through O'Reilly's XSLT book for a few
            > days now, and, despite the typos, it's teaching me a lot. However, real
            > world examples, like those found in this NG, add a depth and challenge to
            > understanding that you just can't get from a book.
            >[/color]

            If you mean Sal Mangano's book -- yes, it is an excellent book. However it
            is not intended for beginners. I would rather recommend Jeni Tenissons
            "beginning XSLT".


            =====
            Cheers,

            Dimitre Novatchev.
            http://fxsl.sourceforge.net/ -- the home of FXSL


            Comment

            Working...