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

标签 xml xslt xinclude

我有一个 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 文件的图像列表?非常感谢您的想法。

提前致谢..!!

已添加:

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

但是当我改变<xsl:template match="@*|node()">时如<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> 

注意事项

此解决方案未实现以下属性: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> 

关于xml - 将不同源位置的 xml 文档图像复制到单个输出目录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11491387/

相关文章:

Python lxml xpath XPathEvalError : Invalid expression -- why?

java - Java 中的 XSLT 转换可以输出无效标签警告吗

xml - 我可以在同一个 XML 文件中使用 xinclude 和实体吗?

c# - 如何使用 LINQ 读取包含 xincluded 文件的 XML?

c# - NodeList.SelectSingleNode() 语法

xml - 是否有针对现有 XML 文件的 XML XQuery 接口(interface)?

jquery - 如何在xsl中使用ip地址

ruby - XInclude 包含元素的命名空间

java - 支持 MOXy @XmlPath 表达式吗?

java - 从 XML 文档、XSLT 和 JAXB 中删除元素