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

1

2

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.

Methedrine answered 15/7, 2012 at 11:13 Comment(0)
H
5

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> 
Harbinger answered 16/7, 2012 at 0:5 Comment(4)
Hi Sean.. Thank you very much..=) If you can please give me this code in XSLT 1.0. I have to use Xsltproc. So I think it is better to use XSLT 1.0. Thank you again..!!Methedrine
This can't be done in XSLT 1.0 without vendor extensions. XSLT 1.0 does still have the document function. If you wanted to limit @parse to 'xml' AND always assume that any included document is guarenteed to exist, well then ... you could do it.Harbinger
That looks great and works well..=) But I need to copy only image path values into a array or some thing like it for future usages of those image paths. This gives other node values also. I modified the question again. Please give me your ideas about how to doing this. I tried lot with changing values in templates. But I couldn't do it.Methedrine
Accept the answer an post another question. But this time show the content of you include documents plus show your expected output.Harbinger

© 2022 - 2024 — McMap. All rights reserved.