Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. XSLT to write links

XSLT to write links

Scheduled Pinned Locked Moved XML / XSL
xml
5 Posts 4 Posters 19 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MS le Roux
    wrote on last edited by
    #1

    Given the following xml:
    <para>The <link url="earth.htm">earth</link> rotates.</para>
    Is there a way to use XSLT to write the content of the <para> element like this:
    <p>The <a href="earth.htm">earth</a> rotates.</p>

    D P 2 Replies Last reply
    0
    • M MS le Roux

      Given the following xml:
      <para>The <link url="earth.htm">earth</link> rotates.</para>
      Is there a way to use XSLT to write the content of the <para> element like this:
      <p>The <a href="earth.htm">earth</a> rotates.</p>

      D Offline
      D Offline
      David Wengier
      wrote on last edited by
      #2

      Something along the lines of:

      <xsl:template match="link">
      <a>
      <xsl:attribute name="href"><xsl:value-of select="@url" /></xsl:attribute>
      <xsl:value-of select="."/>
      </a>
      </xsl:template>

      I think. Its been a while since I have done XSLT. -- David Wengier Sonork ID: 100.14177 - Ch00k

      M 1 Reply Last reply
      0
      • D David Wengier

        Something along the lines of:

        <xsl:template match="link">
        <a>
        <xsl:attribute name="href"><xsl:value-of select="@url" /></xsl:attribute>
        <xsl:value-of select="."/>
        </a>
        </xsl:template>

        I think. Its been a while since I have done XSLT. -- David Wengier Sonork ID: 100.14177 - Ch00k

        M Offline
        M Offline
        MS le Roux
        wrote on last edited by
        #3

        That part works if you only want to show links. What I was wondering was if there was a simple way to put the link inside the rest of the text, i.e. in my example the result I wanted was
        The earth rotates.

        1 Reply Last reply
        0
        • M MS le Roux

          Given the following xml:
          <para>The <link url="earth.htm">earth</link> rotates.</para>
          Is there a way to use XSLT to write the content of the <para> element like this:
          <p>The <a href="earth.htm">earth</a> rotates.</p>

          P Offline
          P Offline
          Paul Watson
          wrote on last edited by
          #4

          MarSCoZa wrote: The earth rotates. Technically that kind of XML is not really valid. e.g. <para> The <link url="earth.htm">earth</link> rotates. </para> The The and Rotates PCDATA sections are "floating". The reason being that an element can either only contain PCDATA or child-elements. It cannot contain both. Think about creating a DTD which defines that... You cannot really. The DTD element definition cannot contain PCDATA and an elements name. However XML is quite forgiving in this case (which is strange considering it's normally very unforgiving nature) and you can use the following XSL to transform it.

          <?xml version="1.0"?>
          <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
          <xsl:template match="/">
          <html>
          <head>
          </head>
          <body>
          <xsl:apply-templates />
          </body>
          </html>
          </xsl:template>

          <xsl:template match="link">
          	<a>
          		<xsl:attribute name="href">
          			<xsl:value-of select="@url" />
          		</xsl:attribute>
          	<xsl:value-of select="."/>
          	</a>
          </xsl:template>
          

          </xsl:stylesheet>

          The key is templates. I used to hate them and thought they were these daft, never used bits of XSL. Until I figured out how they worked and went "oooohh yes!" :) Enjoy! regards, Paul Watson Bluegrass Cape Town, South Africa The greatest thing you'll ever learn is just to love, and to be loved in return - Moulin Rouge

          A 1 Reply Last reply
          0
          • P Paul Watson

            MarSCoZa wrote: The earth rotates. Technically that kind of XML is not really valid. e.g. <para> The <link url="earth.htm">earth</link> rotates. </para> The The and Rotates PCDATA sections are "floating". The reason being that an element can either only contain PCDATA or child-elements. It cannot contain both. Think about creating a DTD which defines that... You cannot really. The DTD element definition cannot contain PCDATA and an elements name. However XML is quite forgiving in this case (which is strange considering it's normally very unforgiving nature) and you can use the following XSL to transform it.

            <?xml version="1.0"?>
            <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
            <xsl:template match="/">
            <html>
            <head>
            </head>
            <body>
            <xsl:apply-templates />
            </body>
            </html>
            </xsl:template>

            <xsl:template match="link">
            	<a>
            		<xsl:attribute name="href">
            			<xsl:value-of select="@url" />
            		</xsl:attribute>
            	<xsl:value-of select="."/>
            	</a>
            </xsl:template>
            

            </xsl:stylesheet>

            The key is templates. I used to hate them and thought they were these daft, never used bits of XSL. Until I figured out how they worked and went "oooohh yes!" :) Enjoy! regards, Paul Watson Bluegrass Cape Town, South Africa The greatest thing you'll ever learn is just to love, and to be loved in return - Moulin Rouge

            A Offline
            A Offline
            Atlantys
            wrote on last edited by
            #5

            Paul Watson wrote: The key is templates. I used to hate them and thought they were these daft, never used bits of XSL. Until I figured out how they worked and went "oooohh yes!" :omg: :omg: I've never used XSLT for XML->HTML, but with XML->XML, templates are the god you bow down to and pray. It boggles my mind the thought of NOT using template matching in XSLT.:omg: :omg:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups