转换合并*.xml [英] Transform and combine *.xml

查看:38
本文介绍了转换合并*.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 xml 文件中提取一系列关系并将它们转换成我用点生成的图形.我显然可以用脚本语言做到这一点,但我很好奇 xslt 是否可行.类似的东西:

I want to pull a series of relationships out of xml files and transform them into a graph I generate with dot. I can obviously do this with a scripting language, but I was curious whether or not this was possible with xslt. Something like:

xsltproc dot.xsl *.xml

这会产生一个像

diagraph {
 state -> state2
 state2 -> state3
 [More state relationships from *.xml files]
}

所以我需要 1) 用diagraph {...}"包装组合的 xml 转换和 2) 能够处理在命令行上指定的任意一组 xml 文档.

So I need to both 1) wrap the combined xml transforms with "diagraph {...}" and 2) be able to handle an arbitrary set of xml documents specified on the command line.

这可能吗?任何指针?

推荐答案

使用 XSLT 2.0 处理器和 collection() 功能这真的很简单.

以下是使用 撒克逊 的示例:

Below is an example using Saxon:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:param name="pDirName" select="'D:/Temp/xmlFilesDelete'"/>

    <xsl:template match="/">
    <wrap>
        <xsl:apply-templates select=
         "collection(
                    concat('file:///',
                            $pDirName,
                            '?select=*.xml;recurse=yes;on-error=ignore'
                             )
                         )/*
          "/>
      </wrap>
    </xsl:template>

    <xsl:template match="*">
      <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>

当此转换应用于任何 XML 文档(未使用)时,它会处理从目录开始的文件系统子树中的所有 xml 文件,其值由全局参数 $pDirName.

When this transformation is applied on any XML document (not used), it processes all xml files in the file-system subtree starting at the directory, whose value is specified by the global parameter $pDirName.

在应用此转换时,那里只有两个 xml 文件:

<apples>3</apples>

<oranges>3</oranges>

产生正确的结果:

<wrap>
   <apples>3</apples>
   <oranges>3</oranges>
</wrap>

这是可以构建的最简单的示例.要完全回答这个问题,可以在调用 Saxon 的命令行上指定目录.阅读有关从命令行调用 Saxon 的方法的更多信息此处.

This is the simplest example possible that can be constructed. To fully answer the question, the directory can be specified on the command line invoking Saxon. Read more about the ways to invoke Saxon from the command-line here.

这篇关于转换合并*.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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