Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 7th, 2018, 02:50 AM
Registered User
 
Join Date: Jun 2018
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Exclamation 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.

Kind Regards,

Flamengo
 
Old June 7th, 2018, 03:26 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 7th, 2018, 04:10 AM
Registered User
 
Join Date: Jun 2018
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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.

Last edited by Flamengo; June 7th, 2018 at 04:29 AM..
 
Old June 7th, 2018, 04:34 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, this isn't a free code-writing service.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
Flamengo (June 7th, 2018)
 
Old June 7th, 2018, 05:13 AM
Registered User
 
Join Date: Jun 2018
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

No Comments.

Last edited by Flamengo; June 7th, 2018 at 05:16 AM..
 
Old June 7th, 2018, 08:44 AM
Registered User
 
Join Date: Jun 2018
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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,





Similar Threads
Thread Thread Starter Forum Replies Last Post
Installing Saxon XSLT Processor booboop BOOK: Beginning XML 3rd Edition 2 April 7th, 2011 12:10 PM
Saxon 9HE and xquery for loops apollo13 XSLT 7 April 6th, 2010 07:02 AM
Which XSLT processor? li72 XSLT 1 August 16th, 2007 11:18 AM
what is this error generated by saxon processor? muki XSLT 8 February 12th, 2007 03:57 PM
Regarding the XSLT Processor sunrain XSLT 5 December 3rd, 2006 03:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.