【问题标题】:Using XSLT to get image metadata使用 XSLT 获取图像元数据
【发布时间】: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)

但是,我似乎无法让它为我工作。我正在尝试将 &lt;size&gt; 元素添加到原始 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,所以完全迷路了。也许这不像我希望的那样简单?我正在尝试的可能吗?

标签: java xml xslt saxon


【解决方案1】:

我认为您的第一个问题可能是您没有找到文档。您试图通过从另一个 StackOverflow 帖子中复制您不理解的代码来做到这一点,并且复制您不理解的代码并修改它以做一些稍微不同的事情,这绝不是一个非常有效的策略。

我猜你要使用的 ImageInfo 类大概是

org.apache.commons.imaging.ImageInfo

但我不熟悉那个类,如果不做一些研究就不能告诉你如何使用它。我已经做了足够的研究来确定它记录在这里:

https://commons.apache.org/proper/commons-imaging/

这给出了一些示例,例如如何提取图像的元数据。恐怕这些示例都是 Java 代码,但您不太可能找到其他任何东西。它们会向您显示所需的调用顺序。

关于如何调用 Java 方法的 Saxon 文档在这里: http://www.saxonica.com/documentation/index.html#!extensibility/functions

您需要确保:

(a) 您已下载并安装了您正在调用的任何 Java 库

(b) 当您调用 Saxon 时,此库的 JAR 文件位于类路径中

(c) 调用函数时使用完整的类名作为命名空间,例如

xmlns:ImageInfo="java:org.apache.commons.imaging.ImageInfo"

很抱歉,如果您没有 Java 经验,这会有点令人生畏。

【讨论】:

  • 感谢您的回复!你是绝对正确的;不幸的是,我遇到了一个问题需要解决,但没有时间正确地学习新的东西来做这件事。我很清楚我进得太高太快了。但是,这些信息应该对我有所帮助,我对此表示感谢。
猜你喜欢
  • 2013-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 1970-01-01
相关资源
最近更新 更多