转换并组合 *.xml

2024-04-08

我想从 xml 文件中提取一系列关系,并将它们转换为我用 dot 生成的图表。显然我可以使用脚本语言来完成此操作,但我很好奇这是否可以使用 xslt 实现。就像是:

xsltproc dot.xsl *.xml

这会产生一个类似的文件

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

因此,我需要 1)用“diagraph {...}”包装组合的 xml 转换,2)能够处理命令行上指定的任意一组 xml 文档。

这可能吗?有什么指点吗?


使用XSLT 2.0 http://www.w3.org/TR/xslt20/处理器和collection() http://www.w3.org/TR/xpath-functions/#func-collection功能这真的很简单.

下面是一个使用的示例Saxon http://www.saxonica.com/documentation/index/intro.html:

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

应用此转换时,只有两个 xml 文件 there:

<apples>3</apples>

and

<oranges>3</oranges>

产生了正确的结果:

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

这是可以构建的最简单的示例。要完全回答这个问题,可以在调用 Saxon 的命令行上指定目录。阅读有关从命令行调用 Saxon 的方法的更多信息here http://www.saxonica.com/documentation/using-xsl/commandline.html.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

转换并组合 *.xml 的相关文章

随机推荐