Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can XSL do it?

Status
Not open for further replies.

kclan

Programmer
Joined
Mar 25, 2004
Messages
3
Location
HK
How can I use XSL to view the following XML like this:

1<font color="0000FF">2</font>3<font color="0000FF">4</font>5

============= test.xml =====================
<?xml version = "1.0"?>
<?xml:stylesheet type = "text/xsl" href = "test.xsl"?>

<!DOCTYPE format [
<!ELEMENT p ( #PCDATA | s )* >
<!ELEMENT s ( #PCDATA ) >
]>

<p>
1
<s>2</s>
3
<s>4</s>
5
</p>

=============== test. xsl ======================

<?xml version="1.0" ?>
<xsl:stylesheet version = "1.0" xmlns:xsl = "
<xsl:template match="p">
<xsl:value-of select="."/>
<xsl:for-each select="COREF">
<font COLOR="#0000FF">
<xsl:value-of select="."/>
</font>
</xsl:for-each>
<xsl:value-of select="."/>
</xsl:template>

</xsl:stylesheet>
 
This should do it:

Code:
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0">
	<xsl:output method="xml" indent="no" omit-xml-declaration="yes"/>
	
	<xsl:template match="/">
		<xsl:apply-templates/>
	</xsl:template>

	<xsl:template match="p">
		<xsl:apply-templates/>
	</xsl:template>

	<xsl:template match="s">
		<font color="0000FF">
			<xsl:value-of select="."/>
		</font>
	</xsl:template>

	<xsl:template match="text()">
		<xsl:value-of select="."/>
	</xsl:template>
</xsl:stylesheet>
 
If you want the output to be exactly as you show, then
a couple of changes to the previous template are
necessary.

Code:
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0">
    <xsl:output method="xml" indent="no" omit-xml-declaration="yes"/>
    
    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="p">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="s">
        <font color="0000FF"><xsl:value-of select="."/></font>
    </xsl:template>

    <xsl:template match="text()">
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>
</xsl:stylesheet>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top