manishekhawat
manishekhawat

Reputation: 709

XML to XML transformation with hyperlink attribute

I have an XML input which is similar to :

<data>
 <document docid="docfilename" />
 <record>ABC</record>
 <info>Testing</info>
 <case docid="casefilename">
  <details>
   Some random information
  </details>
  <author>Creator</author>
 </case>
 <page docid="pageurl">M Page</page>
</data>

The problem is, I want to transform this XML to exactly the same XML which would open in a browser and people could view the transformed XML, with just the docid attribute value replaced with an hyperlink to view the docfilename/casefilename/pageurl depending upon the link.

For e.g.

<data>
 <document docid="<a href="docfilename.html">docfilename</a>" />
 <record>ABC</record>
 <info>Testing</info>
 <case docid="<a href="casefilename.html">casefilename</a>">
  <details>
   Some random information
  </details>
  <author>Creator</author>
 </case>
 <page docid="<a href="http://www.example.com">pageurl</a>">M Page</page>
</data>

In the above output, you are able to view the tag in the docid but user will be seeing only a link in the docid wherever found with a link to the file in the browser.

Any help to transform this xml in given format would be highly appreciated.

Upvotes: 0

Views: 840

Answers (1)

Flynn1179
Flynn1179

Reputation: 12075

So what you're basically asking is how do you create hyperlinks that will show up when an XML document is opened in a browser?

There's no way to do this directly. A browser should only ever display the data in the document, it won't ever create hyperlinks like that.

You're only viable solution is to transform it to an actual HTML document that transforms every element/attribute into text, and overrides the attributes you want hyperlinks for. Here's a simple sheet that will do this:

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

  <xsl:template match="/">
    <html>
      <head>
        <title>XML source</title>
      </head>
      <body>
        <pre><xsl:apply-templates /></pre>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="node()">
    <xsl:value-of select="concat('&lt;',name())" />
    <xsl:apply-templates select="@*" />
    <xsl:text>&gt;</xsl:text>
    <xsl:apply-templates select="node()" />
    <xsl:value-of select="concat('&lt;/',name(),'&gt;')" />
  </xsl:template>

  <xsl:template match="text()">
    <xsl:value-of select="." />
  </xsl:template>

  <xsl:template match="@*">
    <xsl:value-of select="concat(' ',name(),'=&quot;',.,'&quot;')" />
  </xsl:template>

  <xsl:template match="@docid">
    <xsl:variable name="url" select="concat(.,'.html')" />
    <xsl:value-of select="concat(' ',name(),'=&quot;')" />
    <a href='{$url}'><xsl:value-of select="." /></a>
    <xsl:text>&quot;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

There won't be any pretty XML formatting or collapsing/expanding of nodes doing this, but you'll at least be able to display the XML with hyperlinks as needed. As @lwburk said, you haven't really specified where those links should come from; You should be able to adapt this to your needs though.

Upvotes: 1

Related Questions