【发布时间】:2011-11-01 22:25:09
【问题描述】:
我需要解决一个非常奇怪的问题。我需要一个 XSLT 样式表,它将打印结构未知的 xml 文档的元素列表及其属性。经过多次尝试,我设法创造了这样一个东西:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML>
<title></title>
<body>
<xsl:call-template name="recurs">
<xsl:with-param name="nextnodes" select="child::*" />
</xsl:call-template>
</body>
</HTML>
</xsl:template>
<xsl:template name="recurs">
<xsl:param name="nextnodes" />
<xsl:for-each select="$nextnodes">
<xsl:if test="not(name(current())=name(following::*)) and not(name(current())=name(following::*/descendant::*)) ">
Element <b><xsl:value-of select="name(current())" /></b> has attributes <text> </text>
<xsl:for-each select="@*">
<xsl:if test="position()=last()">
<b><xsl:value-of select="name(current())" /><text>.</text></b>
</xsl:if>
<xsl:if test="position()!=last()">
<b><xsl:value-of select="name(current())" /><text>, </text></b>
</xsl:if>
</xsl:for-each>
<br /><br />
</xsl:if>
<xsl:call-template name="recurs">
<xsl:with-param name="nextnodes" select="child::*" />
</xsl:call-template>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
对于这样的测试用例,当元素书再次出现在其他元素中时,它可以正常工作:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="task3_4a.xsl"?>
<catalog subnodes="2">
<cities country="England">
<city name="London" region="London" population="10000" />
<city name="New South Wales" region="Wales" population="800000" />
</cities>
<articles country="USA">
<article name="My lovely country" src="art1.txt" />
<article name="Places to visit" src="art2.txt" />
<article name="Article 3" src="art3.txt" />
</articles>
<books>
<book title="Warhammer">
</book>
<book title="We fought for truth">
</book>
</books>
<scientifics atr = " ">
<book title="Warhammer">
</book>
</scientifics>
</catalog>
但是当我尝试另一个测试时,书籍内有元素文章,它无法正确管理 xml:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="task3_4a.xsl"?>
<catalog subnodes="2">
<cities country="England">
<city name="London" region="London" population="10000" />
<city name="New South Wales" region="Wales" population="800000" />
</cities>
<articles country="USA">
<article name="My lovely country" src="art1.txt" />
<article name="Places to visit" src="art2.txt" />
<article name="Article 3" src="art3.txt" />
</articles>
<books>
<book title="Warhammer">
<article name="My lovely country" src="art1.txt" />
</book>
<book title="We fought for truth">
<article name="My lovely country" src="art1.txt" />
</book>
</books>
<scientifics atr = " ">
<book title="Warhammer">
<article name="My lovely country" src="art1.txt" />
</book>
</scientifics>
</catalog>
输出现在包含字符串“元素文章具有属性名称,src。” 3次。而且我不知道如何解决它......
【问题讨论】:
-
它遍历 3 个可能的节点层次结构以到达 节点。这就是为什么它显示3次。您需要跟踪以某种方式找到的结构,然后进行比较以确保您没有重复。 1.
2. 3. -
您需要打印出每个独特的元素及其属性吗?还是所有元素?
-
我需要将文档中的所有元素与它们的属性一起打印,但是尽管某些元素在不同层次结构级别的文档中出现的次数不止一次,但对于任何此类,输出列表中应该只有一个项目元素。
-
@ferbolg 对于必须累积并与以前的值进行比较的那种操作(只是为了查看是否已经显示),XSLT 可能不是最佳解决方案。 XSLT 非常适合格式化和转换,但对于计数和比较,使用支持地图/字典的脚本语言可能会更好
-
我现在意识到某种 DOM 或 SAX 解析会更好,但问题是我需要一个 XSLT 样式表...