Copy xml document's images in different source locations into single output directory

╄→гoц情女王★ 提交于 2019-12-30 11:14:20

问题


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

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.

Thanks in advance..!!

Added:

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.

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.

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.


回答1:


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> 

Caveats

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

Crippled XSLT 1.0 variant

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


来源:https://stackoverflow.com/questions/11491387/copy-xml-documents-images-in-different-source-locations-into-single-output-dire

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!