p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Call external .class on Saxon 9HE Processor XSLT (http://p2p.wrox.com/showthread.php?t=99905)

Flamengo June 7th, 2018 02:50 AM

Call external .class on Saxon 9HE Processor XSLT
 
Hi Guys,

I really need some help. I have a requirement to make Luhn Algorithm in one tag of the input XML.

I really could not find anything clear on pure XSLT code, but I already developed a java code and I just need to call it on XSLT.

I'm using the processor SAXON 9HE because also from input XML I need slipt into many output XML's.

As I mentioned before this tag on XML that I need use LUHN Algoritm input xml (This is just sample, the input XML and Java it's big to post here):

<?xml version="1.0" encoding="UTF-8"?>
<tag>urn:epc:id:sgtin:0614141.100013.00000000000 1</tag>

I have the java code read:

public class Luhn {
public static String LuhnAlgorithm(String card) {
.....
}

The code is runing perfect but now I need call the function LuhnAlgorithm inside XSLT to make the transformation:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="java:sumCheck.Luhn">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<xmlLuhn>
<xsl:variable name="Luhn13Digits"
select="translate(concat('0',substring(.,19,13 )),'.','')"/>
<xmlJava>
<xsl:value-of select="java:LuhnAlgorithm($Luhn13Digits)"/>
</xmlJava>
</xmlLuhn>
</xsl:template>


Any Help I really appreciate. Thanks for your attention. [:confused:]

Kind Regards,

Flamengo

mhkay June 7th, 2018 03:26 AM

Information on writing Java extension functions for calling from Saxon XSLT code can be found here:

http://www.saxonica.com/documentatio...!extensibility

The mechanism of "reflexive extension functions" which allows you to call directly into your Java code is not available in Saxon-HE. But you can wrap it up as an "integrated extension function".

But really, the Luhn algorithm can be implemented as an XSLT function just as easily as in Java.

Flamengo June 7th, 2018 04:10 AM

mhkay

Thanks for fast replay.

I will check this about java, for you it's easier can you provide a real code ? Not this codes from Google about UPC etc because no works for me any.

I'm really trying code manually this math formula but start be hard.

mhkay June 7th, 2018 04:34 AM

Sorry, this isn't a free code-writing service.

Flamengo June 7th, 2018 05:13 AM

No Comments.

Flamengo June 7th, 2018 08:44 AM

Solved via xslt:

<xsl:variable name="Luhn13Digits" select="translate(concat('0',substring(.,19,13 )),'.','')"/>
<xsl:variable name="restDigits" select="translate(concat('0',substring(.,32,13 )),'.','')"/>
<xsl:variable name="PairNumbers"
select="(((substring($Luhn13Digits, 2, 1)) + (substring($Luhn13Digits,4,1)) +
(substring($Luhn13Digits, 6, 1)) + (substring($Luhn13Digits, 8,1)) +
(substring($Luhn13Digits, 10, 1)) + (substring($Luhn13Digits, 12,1))))"/>
<xsl:variable name="Ludhpos13">
<xsl:choose>
<xsl:when test="substring($Luhn13Digits, 13, 1)*2 &gt; 9">
<xsl:value-of select="substring($Luhn13Digits, 13, 1)*2-9"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($Luhn13Digits, 13, 1)*2"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="Ludhpos11">
<xsl:choose>
<xsl:when test="substring($Luhn13Digits, 11, 1)*2 &gt; 9">
<xsl:value-of select="substring($Luhn13Digits, 11, 1)*2-9"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($Luhn13Digits, 11, 1)*2"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="Ludhpos9">
<xsl:choose>
<xsl:when test="substring($Luhn13Digits, 9, 1)*2 &gt; 9">
<xsl:value-of select="substring($Luhn13Digits, 9, 1)*2-9"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($Luhn13Digits, 9, 1)*2"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="Ludhpos7">
<xsl:choose>
<xsl:when test="substring($Luhn13Digits, 7, 1)*2 &gt; 9">
<xsl:value-of select="substring($Luhn13Digits, 7, 1)*2-9"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($Luhn13Digits, 7, 1)*2"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="Ludhpos5">
<xsl:choose>
<xsl:when test="substring($Luhn13Digits, 5, 1)*2 &gt; 9">
<xsl:value-of select="substring($Luhn13Digits, 5, 1)*2-9"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($Luhn13Digits, 5, 1)*2"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="Ludhpos3">
<xsl:choose>
<xsl:when test="substring($Luhn13Digits, 3, 1)*2 &gt; 9">
<xsl:value-of select="substring($Luhn13Digits, 3, 1)*2-9"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($Luhn13Digits, 3, 1)*2"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="Ludhpos1">
<xsl:choose>
<xsl:when test="substring($Luhn13Digits, 1, 1)*2 &gt; 9">
<xsl:value-of select="substring($Luhn13Digits, 1, 1)*2-9"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($Luhn13Digits, 1, 1)*2"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="OddNumbers" select="$Ludhpos1+$Ludhpos3+$Ludhpos5+$Ludhpos7+$L udhpos9+$Ludhpos11+$Ludhpos13"/>
<xsl:variable name="LuhnMod10" select="$PairNumbers+$OddNumbers"/>

The rest..

Regards,


All times are GMT -4. The time now is 11:09 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.