This is my XML file and If i run the XSLt file i'll get the same output in the same order exactly in Eclipse XSL Transformation.
Even if add a new record to my xml file and run the XSL file, the <xsl:value-of select="generate-id(.)"/>
will create unique id for the new record.
<?xml version="1.0" encoding="UTF-8"?>
<CONTACTS>
<CONTACT>
<Customer-ID>N65539</Customer-ID>
<FirstName>Ben</FirstName>
<LastName>Foden</LastName>
<email></email>
<address></address>
<state>AZ</state>
<country>US</country>
</CONTACT>
<CONTACT>
<Customer-ID>N65539</Customer-ID>
<FirstName>Nimal</FirstName>
<LastName>Anup</LastName>
<email>nimal.anup@gmail.com</email>
<address></address>
<state>TN</state>
<country>IN</country>
</CONTACT>
<CONTACTS>
This is my updated XSLT file:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<!--Identity template to copy content forward-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="CONTACT">
<xsl:copy>
<Customer-ID>
<xsl:apply-templates select="node()" mode="generate-id"/>
</Customer-ID>
<FirstName>
<xsl:value-of select="FirstName"/>
</FirstName>
<LastName>
<xsl:value-of select="LastName"/>
</LastName>
<email>
<xsl:value-of select="email"/>
</email>
<address>
<xsl:value-of select="address"/>
</address>
<state>
<xsl:value-of select="state"/>
</state>
<country>
<xsl:value-of select="country"/>
</country>
</xsl:copy>
</xsl:template>
<xsl:template match="node()" mode="generate-id">
<xsl:text>N</xsl:text>
<xsl:number level="single" count="node()" format="100"/>
</xsl:template>
</xsl:stylesheet>
Then I used the same XSLT file for XSLT processor function in XUL, which I'm getting a different type of ID and output. It's keep generating a new ID for old record and for new record If i add a new record in XML file.
How do i generate a new id only for the new record? and how can i have the same XML template of my input file to my XML output file.
This is the output i'm getting:
<?xml version="1.0" encoding="UTF-8"?>
<CONTACTS>
<CONTACT><Customer-ID>id0x03e4a620</Customer-ID><FirstName>Ben</FirstName><LastName>Foden</LastName><email></email><address></address><state>AZ</state><country>US</country></CONTACT>
<CONTACT><Customer-ID>id0x03e4ad80</Customer-ID><FirstName>Nimal</FirstName><LastName>Anup</LastName><email>nimal.anup@gmail.com</email><address></address><state>TN</state><country>IN</country></CONTACT>
<CONTACTS>
This is my Javascript to call the XSLT file: The script call this function after saving the XML file. The new record will update in the same as mentioned in the input xml file.
function process()
{
var src = readFile("c:\\idgenerator.xsl");
var parsed = (new DOMParser()).parseFromString(src, "text/xml");
var stylesheet = parsed.documentElement;
var processor = new XSLTProcessor();
processor.importStylesheet(stylesheet );
objXMLDoc = processor.transformToDocument(objXMLDoc);
var serializer = new XMLSerializer();
var prettyString = serializer.serializeToString(objXMLDoc);
saveFile(prettyString, "C:\\mercredi.xml");
}
Thank you very much.