Dear All,

I'm a newbie to both XML and XLST. I have an XML document that has the following code

<?xml version="1.0" encoding="WINDOWS-1256"?>
<?xml-stylesheet type="text/xsl" href="NameEntity.xsl"?>
<DOC>
We are going to 
<ENAMEX Type="LOC">washington</ENAMEX>
, after that we will go to 
<ENAMEX Type="MISC">Newyork</ENAMEX>
city, then we will go to
<ENAMEX Type="LOC">England</ENAMEX>
to see our friends.
</DOC>

I want to open this file with internet explorer having the words inside "ENAMEX" tag colored with different colors according to there text (for example if Type='LOC' color red if type = 'MISC' color blue)

I tried to make a code like:

<?xml version="1.0" encoding="WINDOWS-1256"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
 <body style="font-family:Arial;font-size:12pt;background-color:white">
           <xsl:for-each select = "DOC/ENAMEX">

		<xsl:if test="@Type = 'LOC'">
		<Font Color="Red"><xsl:value-of select="."/></Font>
                </xsl:if>
                <xsl:if test="@Type = 'MISC'">
		<Font Color="Green"><xsl:value-of select="."/></Font>
                </xsl:if>
           </xsl:for-each>

          <xsl:value-of select= "DOC"/>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

but I don't want it that way I want to have the output in the internet explorer like this:

"We are going to washington, after that we will go to Newyork city, then we will go to England to see our friends."

Thanks in advance,

ther is a problem to solve
tag DOC because thernnot enought tag

when modify your xml datei to

<?xml version="1.0" encoding="WINDOWS-1256"?>
<?xml-stylesheet type="text/xsl" href="NameEntity.xsl"?>
<DOC>
    <Text> We are going to </Text>
    <ENAMEX Type="LOC">washington</ENAMEX>
    <Text> , after that we will go to </Text>
    <ENAMEX Type="MISC">Newyork</ENAMEX>
    <Text> city, then we will go to </Text>
    <ENAMEX Type="LOC">England</ENAMEX>
    <Text> to see our friends. </Text>
</DOC>

thus

<?xml version="1.0" encoding="WINDOWS-1256"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" method="html"/>
    <xsl:template match="/">
        <html>
            <head>
                <title>ddd</title>
            </head>
            <body>
                <xsl:apply-templates select="DOC"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="DOC">
        <div>
            <xsl:apply-templates select="Text|ENAMEX"/>
        </div>
    </xsl:template>
    <xsl:template match="Text">
        <xsl:value-of select="."/>
    </xsl:template>
    <xsl:template match="ENAMEX">
        <xsl:choose>
            <xsl:when test="@Type='LOC'">
                <font color="red">
                    <xsl:value-of select="."/>
                </font>
            </xsl:when>
            <xsl:when test="@Type='MISC'">
                <font color="green">
                    <xsl:value-of select="."/>
                </font>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

result

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ddd</title>
</head>
<body>
<div> We are going to <font color="red">washington</font> , after that we will go to <font color="green">Newyork</font> city, then we will go to <font color="red">England</font> to see our friends. </div>
</body>
</html>

sorry for my bad english

Thanks a lot for your reply. It helped me a lot. But I want to ask one more question: Is there any way that I can do this without adding the tag? I have a lot of data that is tag with the scheme that I have mentioned before. It will be hard to modify. Do you mean that the way by which the XML file is written is incorrect?.

Thanks in advance,

use a sax parser
in Java example

package javaapplication2;
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;

public class Main extends DefaultHandler
{
    private boolean tag = false;
    private int level = 0;
    private String xmltext= "";
    private String nameAusgabedatei;
    private File ausgabedatei;
    private FileWriter fw;
    private BufferedWriter bw;
    public Main(String arg1)
    {
      nameAusgabedatei=arg1;
    }

    public void indent()
    {
        for (int i=0;i<level;i++)
            System.out.print("      ");
        //xmltext+="    ";
    }

    @Override
    public void startDocument() throws SAXException
    {    
        System.out.println("<html>");
        xmltext+="<html>\n";
        
        level++;
        indent();
        System.out.println("<body>");
        xmltext+="<body>\n";
        level++;
        indent();
    }
    @Override
    public void endDocument() throws SAXException
    {
        level--;
        indent();
        System.out.println("</body>");
        xmltext+="</body>\n";
        level--;
        indent();
        System.out.println("</html>\n");
        xmltext+="</html>";
        try{
      ausgabedatei = new File(nameAusgabedatei);
      fw = new FileWriter(ausgabedatei);
      bw = new BufferedWriter(fw);
      bw.write(xmltext);
      bw.close();
      }
    catch (ArrayIndexOutOfBoundsException aioobe) {
      System.out.println("Aufruf mit: java SchreibeDatei name");
      System.out.println("erzeugt eine Datei name.html");
    }
    catch (IOException ioe) {
      System.out.println("Habe gefangen: "+ioe);
    }



    }
    @Override
    public void startElement(String namespaceURI, String localName,
            String qName, Attributes atts) throws SAXException
    {   
        if (qName.equals("ENAMEX") && (atts.getValue(0).equals("MISC"))){
            level++;
            indent();
            System.out.print("<font color=\"green\">");
            xmltext+="<font color=\"green\">\n";
            tag=true;

        }
        if (qName.equals("ENAMEX") && (atts.getValue(0).equals("LOC"))){
            level++;
            indent();
            xmltext+="<font color=\"red\">\n";
            tag=true;

        }
        if (qName.equals("DOC")){
            System.out.print("<div>");
            xmltext+="<div>\n";
        }
    }
    @Override
    public void endElement(String namespaceURI, String localName, String qName)
    {   
        if (qName.equals("ENAMEX")){
            
            System.out.println("</font>");
            xmltext+="</font>\n";
            tag = false;
            level--;
            indent();

        }
        if (qName.equals("DOC")){
            indent();
            System.out.println("</div>");
            xmltext+="</div>\n";

        }
        
    }
    @Override
    public void characters(char ch[], int start, int length)
    {
        String s = new String(ch,start,length).trim();
        if (s.length() > 0) {
           
            if (tag){
            System.out.print(s);
            xmltext+=s;
            }
            else{            
            System.out.println(s);
            xmltext+=s;
            xmltext+="\n";
            }
        }
    }
    @Override
    public void ignorableWhitespace(char[] ch, int start, int length)
    {
    }
    public String xml(){
        return xmltext;
    }
    public static void main(String args[])
    {        
        try {
            SAXParserFactory factory   = SAXParserFactory.newInstance();
            SAXParser        saxParser = factory.newSAXParser();
            saxParser.parse(args[1],new Main(args[0]));
            }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

to use java Main out.html in.xml

I HOPE THIS WILL HELP YOU

Thanks for your great effort.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.