[XML-SIG] Hello again.

Matt Gushee Matt Gushee <mgushee@havenrock.com>
Tue, 9 Apr 2002 01:12:57 -0600


On Tue, Apr 09, 2002 at 12:30:20AM -0600, Matthew D. Wood wrote:

> I am creating an XML document that will contain several web-links.  In
> trying to figure out the best way to represent a web-link I've pretty
> much convinced myself that the best method will be the standard:
> 	<a href="www.webpage.com">Look at the pretty page!</a>
> style of link.  This makes sense to me as it's valid XML and it should
> be pretty easy to convert it to html when I need to.

Well, to be exact, it already is HTML, as well as being well-formed*
XML.

> Now, I would like to use XSLT to transform my XML document into an HTML
> or XHTML document.

Generally speaking, these sorts of questions are best asked on the
xsl-list (http://www.mulberrytech.com/xsl/xsl-list/index.html). Several
of the world's leading XSL/XSLT experts are regularly on that list
answering questions. But since I know the answer, I'll let it slide
this time ;-)

> should work in order to accomplish this, but I have no idea how to
> preserve a portion of the XML tree.  I want to totally recreate the
> entire <a ...>...</a> tree, and not change a thing.  I can't for the
> life of me figure out how to do this.  

It's easy, though maybe not obvious. When you want to copy elements from
the source to the output, you can use either an <xsl:copy> or an
<xsl:copy-of> instruction.

<xsl:copy/> will give you a copy of the context node. Period. So this
  form is not usually what you want. If you do

<xsl:copy>
  <xsl:apply-templates/>
</xsl:copy>

then you'll get the node, and send its contents back for further 
processing -- which may consist of more <xsl:copy>s.

<xsl:copy select="."/> will copy the context node (or whatever you
  specify in the select attribute), and automatically recurse down
  through its descendants, copying all elements and text nodes. But
  in this case, you want to copy the attributes, so this one won't
  work for you.

> <root>
>   <section>
>     <title>first section</title>
>     <a href="www.some-page.com">some page</a>
>   </section>
> 
>   <section>
>     <title>second section</title>
>     <a href="www.another-page.org">a different page</a>
>   </section>
> </root>
> 
> 
> My desired html:
> 
> <html>
>   <head>
>     <title>a good title</title>
>   </head>
> 
>   <body>
>     <a href="www.some-page.com">some page</a>
>     <a href="www.another-page.org">a different page</a>
>   </body>
> </html>

Okay, so you will need something like this:

  <xsl:template match="root">
    <body>
      <!-- This select statement assumes that all <a/> elements you
           are interested in are children of <section/> elements,
           which are children of the current node. -->
      <xsl:apply-templates select="section/a"/>
    </body>
  </xsl:template>

  <xsl:template match="a">
    <xsl:copy>
      <!-- This copies all attributes. --> 
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

> And for that matter, I haven't figured out how to add an attribute to
> the target XML.

If the value is a literal string, you just add it:

  <a href="www.some-page.com" id="foo-bar-baz-boom-bah">

Or you can stick in a variable reference or XPath expression by using
an Attribute Value Template (AVT):

  <a href="ww.some-page.com" thingie="{$my_variable}"/>

That should hold you for a bit. If you plan to do any serious XSLT work,
I'd suggest:

  1) Investing in a good book -- I like the _XSLT Programmer's Reference_,
     by Michael Kay (Wrox Press). It is a reference, though. If you need
     a tutorial, there's a recently-published book from O'Reilly ... I
     forget the exact title; and
  2) Subscribing to XSL-LIST. Michael Kay, BTW, is one of the experts who
     hang out on the list.
  
Best of luck.

-- 
Matt Gushee
Englewood, Colorado, USA
mgushee@havenrock.com
http://www.havenrock.com/