gpt4 book ai didi

xslt - 转换合并*.xml

转载 作者:行者123 更新时间:2023-12-04 22:53:53 25 4
gpt4 key购买 nike

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

xsltproc dot.xsl *.xml

这将产生一个文件
diagraph {
state -> state2
state2 -> state3
[More state relationships from *.xml files]
}

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

这可能吗?任何指针?

最佳答案

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

下面是一个使用 的例子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 .

在应用此转换时,只有两个 xml 文件 那里:
<apples>3</apples>


<oranges>3</oranges>

产生了正确的结果 :
<wrap>
<apples>3</apples>
<oranges>3</oranges>
</wrap>

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

关于xslt - 转换合并*.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/425117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com