将不同源位置的 xml 文档图像复制到单个输出目录中

2023-11-29

我有一个 xml 文档,其中使用 xinclude 来访问其他几个 xml 文件。

<chapter xml:id="chapter1">
<title>Chapter in Main Doc</title>
<section xml:id="section">
    <title>Section in Main Doc 1</title>
            <mediaobject>
                <imageobject>
                    <imagedata fileref="images/car.jpg"/>
                </imageobject>
            </mediaobject>
</section>
<xi:include href="../some-doc/section1.xml"/>
<xi:include href="../some-doc/section2.xml"/>

这些其他section1和section2 xml文件在不同的源位置使用不同的图像。我需要将所有图像复制到单个输出目录。因此,首先,我计划使用 XSLT 来解析整个 xml 文档并生成要复制的图像列表。如何使用 XSLT 生成 xml 文件的图像列表?非常感谢您的想法。

提前致谢..!!

Added:

我尝试使用下面回答的 XSLT 1.0 代码。当我使用它生成 html 输出时,它只显示章节 ID,例如“chapter1,section ...”。它不显示 imagedata 节点内的图像路径值。

但当我改变了<xsl:template match="@*|node()"> as <xsl:template match="*">然后它还会显示 xincluded xml 文件的所有图像路径值。但也有其他节点的值,如上所示。我需要过滤除图像路径之外的所有值。

在这里,我只需要复制所有 xml 文档的图像路径,并将这些所有路径保存在数组或类似的东西中。然后我可以使用 java 类将这些保存的图像路径用于图像处理目的。


这不是一个完整的解决方案,但可能足以满足您的需求。以下 XSLT 2.0 样式表复制一个文档,扩展 XIncludes(下面注明了注意事项)。

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xi="http://www.w3.org/2001/XInclude"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  exclude-result-prefixes='xsl xi fn'>
<xsl:output method="xml" indent="yes"/>

<xsl:template match="@*|node()">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>

<xsl:template match="xi:include[@href][@parse='xml' or not(@parse)][fn:unparsed-text-available(@href)]">
 <xsl:apply-templates select="fn:document(@href)" />
</xsl:template>

<xsl:template match="xi:include[@href][@parse='text'][fn:unparsed-text-available(@href)]">
 <xsl:apply-templates select="fn:unparsed-text(@href,@encoding)" />
</xsl:template>

<xsl:template match="xi:include[@href][@parse=('text','xml') or not(@parse)][not(fn:unparsed-text-available(@href))][xi:fallback]">
 <xsl:apply-templates select="xi:fallback/text()" />
</xsl:template>

<xsl:template match="xi:include" />

</xsl:stylesheet> 

Caveats

该解决方案没有实现以下属性:xpointer、accept 和accept-language。

残缺的 XSLT 1.0 变体

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xi="http://www.w3.org/2001/XInclude"
  exclude-result-prefixes='xsl xi'>
<xsl:output method="xml" indent="yes"/>

<xsl:template match="@*|node()">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>

<xsl:template match="xi:include[@href][@parse='xml' or not(@parse)]">
 <xsl:apply-templates select="document(@href)" />
</xsl:template>

<xsl:template match="xi:include" />

</xsl:stylesheet> 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将不同源位置的 xml 文档图像复制到单个输出目录中 的相关文章

随机推荐