我想从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处理器和
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来指定目录.从命令行here了解有关调用Saxon的方法的更多信息.