Input:
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" type="NOIA" xsi:noNamespaceSchemaLocation="sample.xsd">
<id>X17A</id>
<companyName>Foo Bars</companyName>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tincidunt turpis id metus porttitor convallis. Duis ullamcorper magna a est suscipit eget blandit magna ullamcorper. Vivamus sit amet auctor elit.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tincidunt turpis id metus porttitor convallis. Duis ullamcorper magna a est suscipit eget blandit magna ullamcorper. Vivamus sit <url>http://www.google.com/</url> amet auctor elit.</p>
</document>
Desired Output:
<html>
<body>
<h1>Foo Bars Company</h1>
<div><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tincidunt turpis id metus porttitor convallis. Duis ullamcorper magna a est suscipit eget blandit magna ullamcorper. Vivamus sit amet auctor elit.</p></div>
<div><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tincidunt turpis id metus porttitor convallis. Duis ullamcorper magna a est suscipit eget blandit magna ullamcorper. Vivamus sit <a href="http://www.google.com/">http://www.google.com/</a> amet auctor elit.</p></div>
</body>
</html>
Here is as far as I got with the sheet:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes" version="4.0"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="id"></xsl:template>
<xsl:template match="companyName">
<h1><xsl:value-of select="."/></h1>
</xsl:template>
<xsl:template match="url">
<a href="#test"><xsl:value-of select="."/></a>
</xsl:template>
<xsl:template match="p">
<div><p><xsl:value-of select="."/></p></div>
</xsl:template>
</xsl:stylesheet>
I can’t figure out how to put the value in the href=””
Also, this doesn’t end up with an href, I’m just getting the text in a p.
When this XSLT:
…is applied to the originally provided XML:
…the desired result is produced:
Note that this solution will work with either XSLT 1.0 or XSLT 2.0.
Explanation:
The first template is
The Identity Transform. It’s purpose is to copy all nodes and attributes as-is from the source document to the result document.The second template matches any
<url>element found in the document. Upon finding a<url>element, a new<a>element is created, given anhrefattribute that matches the original value of the<url>element (via XSLT’sAVT [Attribute Value Template]functionality), and given a value that matches that original<url>value.Note that if elements other than
<url>need to be replaced in this same manner, you can simply alter thematchattribute of the second template as necessary. For example:The third template matches any
<id>element and, in place of that element, outputs nothing (which effectively deletes that element).The fourth template matches any
<companyName>element. In its place, a new<h1>element is created and given a value that is a concatenation of<companyName>‘s value and ” Company”.The fifth template matches any
<p>element and wraps it in a<div>element.