Bug #5877
Updated by Michael Kay about 1 year ago
**InputXml:** ``` <?xml version=\"1.0\"?> <Guids> <serialNum>1</serialNum> </Guids> <Guids> <serialNum>2</serialNum> </Guids> ``` **Case1:Using functions inside xslt** ``` <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:func="http://www.xsltfunctions.com" xmlns:helper="com.test.xslt.function" extension-element-prefixes="helper" version="2.0"> <xsl:template match="TestXML"> <xsl:element name="TestElement"> <xsl:for-each select="./Guids"> <xsl:element name="serialNum"> <xsl:value-of select="serialNum"/> </xsl:element> <xsl:element name="TestGuid"> <xsl:value-of select="func:genGuid()"/> </xsl:element> </xsl:for-each> </xsl:element> </xsl:template> <xsl:function name="func:genGuid"> <xsl:value-of select="helper:genGUID()"/> </xsl:function> </xsl:stylesheet> **Output ** <?xml version="1.0" encoding="UTF-8"?> <TestElement> <serialNum>1</serialNum> <TestGuid>D5CAF5D4-8C05-408A-B56A-A277A8F33975</TestGuid> <serialNum>2</serialNum> <TestGuid>D5CAF5D4-8C05-408A-B56A-A277A8F33975</TestGuid> </TestElement> ``` **Case2:If I use the custom function directly inside the for-each(instead of template/function) then also it is producing the same guid as above which is not expected.** **Case3:Using template inside Xslt is producing the correct result** ``` <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:helper="com.test.xslt.function" extension-element-prefixes="helper" version="2.0"> <xsl:template match="TestXML"> <xsl:element name="TestElement"> <xsl:for-each select="./Guids"> <xsl:element name="serialNum"> <xsl:value-of select="serialNum"/> </xsl:element> <xsl:element name="TestGuid"> <xsl:call-template name="genGUID"/> </xsl:element> </xsl:for-each> </xsl:element> </xsl:template> <xsl:template name="genGUID"> <xsl:value-of select="helper:genGUID()"/> </xsl:template> </xsl:stylesheet> ``` **Output:** ``` <?xml version="1.0" encoding="UTF-8"?> <TestElement> <serialNum>1</serialNum> <TestGuid>E75AE452-4B7F-4CF8-A6CE-64DE81687384</TestGuid> <serialNum>2</serialNum> <TestGuid>771D2C44-8216-4CA7-B43A-2CBEB03AF7F3</TestGuid> </TestElement> ``` **Customer function implementation:** ``` package com.test.xslt.function; import java.util.UUID; import net.sf.saxon.s9api.ExtensionFunction; import net.sf.saxon.s9api.QName; import net.sf.saxon.s9api.SaxonApiException; import net.sf.saxon.s9api.SequenceType; import net.sf.saxon.s9api.XdmAtomicValue; import net.sf.saxon.s9api.XdmValue; public class GUIDGenFunction implements ExtensionFunction { @Override public QName getName() { return new QName( "com.test.xslt.function", "genGUID" ); } @Override public SequenceType[] getArgumentTypes() { return new SequenceType[]{}; } @Override public SequenceType getResultType() { return SequenceType.ANY; } @Override public XdmValue call( XdmValue[] arguments ) throws SaxonApiException { String result = UUID.randomUUID().toString().toUpperCase(); System.out.println( "GUID Result " + result ); return new XdmAtomicValue( result ); } } ExtensionFunction genGUIDFunction = new GUIDGenFunction(); processor.registerExtensionFunction( genGUIDFunction ); ``` Please let me know why it is not working as expected in case1&2.