Altova XMLSpy 2024 Professional Edition

如果是通过类文件访问,则有四种可能性:

 

类文件在一个包中。XSLT或XQuery文件在Java包所在的文件夹中。(参见下方示例

类文件不在包中。XSLT或XQuery文件在类文件所在的文件夹中。(参见下方示例

类文件在一个包中。XSLT或XQuery文件位于任意位置。(参见下方示例

类文件不在包中。XSLT或XQuery文件位于任意位置。(参见下方示例

 

请考虑以下情况:类文件不在包中,但在XSLT或XQuery文档所在的文件夹中。在这种情况下,不需要指定文件位置,因为所有的类都会在文件夹中找到。以下是标识一个类的语法:

 

         java:classname

 

 其中

 

java:表示一个正被调用的用户定义的Java函数;(当前目录中的Java类将被默认加载)

         classname是所需的方法的类的名称

 

该类是在命名空间URI中标识的,该命名空间被用于为一个方法调用添加前缀。

 

类文件在包中,XSLT/XQuery文件在Java包所在的文件夹中

以下示例调用了com.altova.extfunc包中Car类的getVehicleType()方法。com.altova.extfunc包在JavaProject文件夹中。XSLT文件也在JavaProject文件夹中。

 

 <xsl:stylesheet version="2.0" 

         xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 

         xmlns:xs="http://www.w3.org/2001/XMLSchema" 

         xmlns:fn="http://www.w3.org/2005/xpath-functions"

         xmlns:car="java:com.altova.extfunc.Car" >

 <xsl:output exclude-result-prefixes="fn car xsl fo xs"/>

 

 <xsl:template match="/">

            <a>

    <xsl:value-of select="car:getVehicleType()"/>

    </a>

 </xsl:template>

 

 </xsl:stylesheet>

 

类文件被引用,XSLT/XQuery文件在类文件所在的文件夹中

以下示例调用了Car类的getVehicleType()方法。让我们假设:(i) Car类文件位于以下文件夹:JavaProject/com/altova/extfunc,并且(ii) 该文件夹就是以下示例中的当前文件夹。XSLT文件也在JavaProject/com/altova/extfunc文件夹中。

 

 <xsl:stylesheet version="2.0" 

         xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 

         xmlns:xs="http://www.w3.org/2001/XMLSchema" 

         xmlns:fn="http://www.w3.org/2005/xpath-functions"

         xmlns:car="java:Car" >

 <xsl:output exclude-result-prefixes="fn car xsl fo xs"/>

 

 <xsl:template match="/">

            <a>

    <xsl:value-of select="car:getVehicleType()"/>

    </a>

 </xsl:template>

 

 </xsl:stylesheet>

 

类文件在包中,XSLT/XQuery文件在任意位置

以下示例调用了com.altova.extfunc包中Car类的getCarColor()方法。com.altova.extfunc包在JavaProject文件夹中。XSLT文件在任意位置。在这种情况下,包的位置必须在URI中作为查询字符串指定。语法如下:

 

         java:classname[?path=uri-of-package]

 

 其中

 

java:表示一个正被调用的用户定义的Java函数。

uri-of-package表示Java包的URI。

classname是所需的方法的类的名称。

 

该类是在命名空间URI中标识的,该命名空间被用于为一个方法调用添加前缀。下方示例展示了如何访问一个不在当前目录中的类文件。

 

 <xsl:stylesheet version="2.0" 

         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

         xmlns:xs="http://www.w3.org/2001/XMLSchema" 

         xmlns:fn="http://www.w3.org/2005/xpath-functions"

         xmlns:car="java:com.altova.extfunc.Car?path=file:///C:/JavaProject/" >

 

 <xsl:output exclude-result-prefixes="fn car xsl xs"/>

 

 <xsl:template match="/">

            <xsl:variable name="myCar" select="car:new('red')" /> 

    <a><xsl:value-of select="car:getCarColor(\$myCar)"/></a>

 </xsl:template>

 

 </xsl:stylesheet>

 

类文件被引用,XSLT/XQuery文件在任意位置

以下示例调用了Car类的getCarColor()方法。让我们假设,Car类文件在C:/JavaProject/com/altova/extfunc文件夹中,并且XSLT文件在任意位置。类文件的位置必须在命名空间URI中作为查询字符串指定。语法如下:

 

         java:classname[?path=<uri-of-classfile>]

 

 其中

 

java:表示一个正被调用的用户定义的Java函数。

uri-of-classfile是包含该类文件的文件夹的URI。

classname是所需的方法的类的名称。

 

该类是在命名空间URI中标识的,该命名空间被用于为一个方法调用添加前缀。下方示例展示了如何访问一个不在当前目录中的类文件。

 

 <xsl:stylesheet version="2.0" 

         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

         xmlns:xs="http://www.w3.org/2001/XMLSchema" 

         xmlns:fn="http://www.w3.org/2005/xpath-functions"

         xmlns:car="java:Car?path=file:///C:/JavaProject/com/altova/extfunc/" >

 

 <xsl:output exclude-result-prefixes="fn car xsl xs"/>

 

 <xsl:template match="/">

            <xsl:variable name="myCar" select="car:new('red')" /> 

    <a><xsl:value-of select="car:getCarColor(\$myCar)"/></a>

 </xsl:template>

 

 </xsl:stylesheet>

 

提示:当一个路径是由扩展函数提供的,则该路径将被添加到ClassLoader。

 

© 2017-2023 Altova GmbH