【问题标题】:Create list of image paths in XML documents在 XML 文档中创建图像路径列表
【发布时间】:2023-03-18 18:21:02
【问题描述】:

我有一个包含其他 xml 文件的 xml 文档。所有这些 xml 文件都包含位于不同源位置的图像的相对路径。

<chapter xml:id="chapter1">
    <title>First chapter in Main Document</title>
    <section xml:id="section1">
        <title>Section 1 in Main Document</title>
        <para>this is paragraph<figure>
                <title>Car images</title>
                <mediaobject>
                    <imageobject>
                        <imagedata fileref="images/image1.jpg"/>
                    </imageobject>
                </mediaobject>
            </figure></para>
    </section>
    <xi:include href="../doc/section2.xml"/>
    <xi:include href="../doc/section3.xml"/>
</chapter>

这里是 section2 和 section3 xml 文档的样子。

<section xml:id="section2"  
        <title>Main Documentation Section2</title>
        <para>This is also paragraph <figure>
                <title>Different Images</title>
                <mediaobject>
                    <imageobject>
                        <imagedata fileref="images/image2.jpg"/>
                    </imageobject>
                </mediaobject>
            </figure></para>
    </section>

我想创建 XSLT 1.0 样式表,它将在所有 xml 文档中生成图像路径列表。我要将位于不同源位置的图像复制到单个图像文件夹中。然后我将能够使用该图像路径列表来复制这些图像。如果该图像路径列表保存在可以由 java 类访问的结构中,那就太好了。

目前我正在使用从另一个问题中得到的 XSLT。但是这个 XSLT 提供了其他节点的值以及图像路径。我尝试通过更改模板值来过滤它们。

<xsl:template match="*">
<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>

预期的结果列表会是这样的,

/home/vish/test/images/image1.jpg

/home/vish/test/doc/other/images/image2.jpg

/home/vish/test2/other/images/image3.jpg

提前谢谢..!!

【问题讨论】:

  • “/home/vish/test”部分从何而来?
  • 我想添加 xml:base 以添加该公共路径并且没有绝对路径,我将无法使用 xml 文档中的相对路径将图像复制到输出目录中。但我不确定。
  • document() 函数应该能够很好地解析绝对引用和相对引用。

标签: xml xslt xinclude


【解决方案1】:

怎么样...

<?xml version="1.0" encoding="utf-8"?>
<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:strip-space elements="*" />

<xsl:template match="/">
 <image-paths>
  <xsl:apply-templates select="*" />
 </image-paths>
</xsl:template>

<xsl:template match="*">
 <xsl:apply-templates select="*" />
</xsl:template>

<xsl:template match="imagedata">
 <imagedata fileref="{@fileref}" />
</xsl:template>

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

</xsl:stylesheet>

你应该得到类似...的输出

<image-paths>
 <imagedata fileref="path1/image1.jpg" />
 <imagedata fileref="path2/image2.jpg" />
 <imagedata fileref="path3/image3.jpg" />
</image-paths>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 2016-05-19
    相关资源
    最近更新 更多