XML file -
<Remarks>
<Remark>
<Cid>2009-1</Cid>
<Date>3-11-2011 2:55:0</Date>
<Title>Book</Title>
<Comment>XXX</Comment>
</Remark>
<Remark>
<Cid>2009-2</Cid>
<Date>3-12-2011 2:55:0</Date>
<Title>Song</Title>
<Comment>XXX</Comment>
</Remark>
</Remarks>
I want to replace the text of <Comment>
for particular <Cid>
. For example; for Cid 2009-1,
I want to update/change the respective Comment XXX to YYY. These both values are based on parameters, which my java code will pass. Following is the XSLT file -
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="ciName" select=""/>
<xsl:param name="coName" select=""/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//Remarks/Remark/Comment[preceding-sibling::Cid='$ciName']">
<xsl:copy>
<xsl:value-of select="$coName"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
And this is the part Java code -
String cid = "2009-1";
String comm = "YYY";
TransformerFactory factory = TransformerFactory.newInstance();
Source xslt = new StreamSource(new File("loc1.xslt"));
Transformer transformer = factory.newTransformer(xslt);
transformer.setParameter("ciName",cid);
transformer.setParameter("coName",comm);
Source text = new StreamSource(new File("Comments.xml"));
transformer.transform(text, new StreamResult(new File( "Comments1.xml")));
Error -
ERROR: 'Premature end of file.'
ERROR: 'com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Premature end of file.'
Excep......
Also I am NOT able to edit/replace/change the respective comment... Any help....? Thanks in advance.
With an XSLT 2.0 processor like Saxon 9 you could use
With XSLT 1.0 you are not allowed to use a variable or parameter reference in a match pattern so you need to use