将不同源位置的 xml 文档图像复制到单个输出目录中 [英] Copy xml document's images in different source locations into single output directory

查看:18
本文介绍了将不同源位置的 xml 文档图像复制到单个输出目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having a xml document with uses xinclude for access several other xml files.

<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 文件的图像列表?你的想法真的很感激.

These other section1 and section2 xml files uses different images in different source locations. I need to copy those all images to single output directory. There fore at first, I am planning to use XSLT to parse entire xml documents and generate a list of images to be copied. How can I generate that list of images of xml files using XSLT? Your ideas really appreciated.

提前致谢..!!

添加:

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

I tried with below answered XSLT 1.0 code. When I generate html output using it, it only displays chapter and section ids like "chapter1, section ...". It does not display image path value inside imagedata node.

但是当我将 <xsl:template match="@*|node()"> 更改为 <xsl:template match="*">> 然后它还会显示 xincluded xml 文件的所有图像路径值.但是还有其他节点的值也与上面类似.我需要过滤除图像路径以外的所有值.

But when I changed <xsl:template match="@*|node()"> as <xsl:template match="*"> then it displays all image path values of xincluded xml files also. But there are other node's values like above also. I need to filter all values other than image paths.

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

Here I need to copy only image paths of all xml documents and keep those all paths in a array or some thing like it. Then I can use those saved image paths for image coping purpose using a java class.

推荐答案

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

This is not a complete solution but it may be enough for your needs. The following XSLT 2.0 style-sheet copies a document, expanding out XIncludes (with caveats noted following).

<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> 

注意事项

此解决方案没有实现属性:xpointer、accept 和 accept-language.

Caveats

This solution does not implement the attributes: xpointer, accept and accept-language.

<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> 

这篇关于将不同源位置的 xml 文档图像复制到单个输出目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆