【发布时间】:2017-03-17 15:23:02
【问题描述】:
我有 XML,其中包含指向各种图像文件的链接,如下所示:
<xml>
<image>
<name>fig1.jpg</name>
<type>graphic</type>
<path>C:\some folder\artwork\fig1.jpg</path>
</image>
<image>
<name>fig2.jpg</name>
<type>graphic</type>
<path>C:\some folder\artwork\fig2.jpg</path>
</image>
</xml>
是否可以使用 XSLT 进行处理以获取这些图像的各种元数据,例如分辨率、宽度、高度和文件格式?所以基本上我可以传递路径,它会返回我想要的数据。
我一直在寻找答案,似乎我正在寻找某种 java 扩展函数,但我对 java 一无所知,所以无法真正掌握我需要做什么或如何制作它工作。
有一些 java 函数,例如用于格式化日期的函数,我在其中看到了正在使用的示例,并且它们似乎并不比标准 XSLT 函数更难工作,但是我想这些本质上是处理文本而不是外部图像文件,所以这就是我要解开的地方吗?
我认为我发现最接近我需要做的事情是:Running custom Java functions within XSLT and SAXON (9.1.8)
但是,我似乎无法让它为我工作。我正在尝试将 <size> 元素添加到原始 XML,所以这对我来说似乎是合乎逻辑的:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ImageInfo="java:ImageInfo"
version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="path">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
<size>
<xsl:variable name="image" select="ImageInfo:new(.)"/>
[image] file found:
<xsl:value-of select="ImageInfo:getWidth($image)"/> x
<xsl:value-of select="ImageInfo:getHeight($image)"/>
</size>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Oxygen 给了我这些错误信息:
Stylesheet compilation failed: 3 errors reported
XPST0017 XPath syntax error at char 0 on line 15 in {ImageInfo:new(.)}: Cannot find a matching 1-argument function named {java:ImageInfo}new(). For diagnostics on calls to Java methods, use the -TJ command line option or set the Configuration property FeatureKeys.TRACE_EXTERNAL_FUNCTIONS
XPST0017 XPath syntax error at char 0 on line 17 in {ImageInfo:getWidth($image)}: Cannot find a matching 1-argument function named {java:ImageInfo}getWidth(). For diagnostics on calls to Java methods, use the -TJ command line option or set the Configuration property FeatureKeys.TRACE_EXTERNAL_FUNCTIONS
XPST0017 XPath syntax error at char 0 on line 18 in {ImageInfo:getHeight($image)}: Cannot find a matching 1-argument function named {java:ImageInfo}getHeight(). For diagnostics on calls to Java methods, use the -TJ command line option or set the Configuration property FeatureKeys.TRACE_EXTERNAL_FUNCTIONS
我在 Oxygen 17.1 中使用 Saxon EE,如果有帮助的话。
对于适合熟悉 XSLT 但完全是 java 新手的人的任何帮助或任何指向资源的指针,我们将不胜感激。
【问题讨论】:
-
由于 ImageInfo 类显然是问题的一部分,也许您应该编辑您的问题并包含该类的代码。
-
从哪里获得图像大小,如果它不是 xml 的一部分?你的java代码在哪里?
-
您尝试使用的是这个 ImageInfo 类吗? kickjava.com/src/imageinfo/ImageInfo.java.htm
-
感谢您的回复。由于我不太了解您的查询,因此它建议我需要回归基础,因为我不知道我在这里做什么。我以前从未尝试过使用java,所以完全迷路了。也许这不像我希望的那样简单?我正在尝试的可能吗?