最近我实现了 XSLT,它将任何 XML 转换为 HTML 树视图,表示为嵌套的无序列表。这是此 XSLT 的简化版本:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="." mode="render"/>
</body>
</html>
</xsl:template>
<xsl:template match="/" mode="render">
<div>
<ul>
<xsl:apply-templates mode="render"/>
</ul>
</div>
</xsl:template>
<xsl:template match="*" mode="render">
<li>
<span>
<xsl:value-of select="local-name()"/>
</span>
<div>
<xsl:if test="./text()">
<span>
<xsl:value-of select="translate(./text(),' ',' ')"/>
</span>
</xsl:if>
<xsl:if test="count(child::*)>0 or count(attribute::*)>0">
<ul>
<xsl:apply-templates select="@*" mode="render"/>
<xsl:apply-templates select ="*" mode="render"/>
</ul>
</xsl:if>
</div>
</li>
</xsl:template>
<xsl:template match="@*" mode="render">
<li>
<span>
<xsl:value-of select="local-name()"/>
</span>
<div>
<span>
<xsl:value-of select="translate(.,' ',' ')"/>
</span>
</div>
</li>
</xsl:template>
</xsl:stylesheet>
请注意,此 XSLT 不依赖于 XML 模式。因此它可以应用于任何 XML。例如给定输入 XML
<Catalog xmlns="somenamespace">
<CatalogName>First catalog</CatalogName>
<Products>
<Product id="p1">
<Name>Some product</Name>
<Description>foo</Description>
</Product>
<Product id="p2">
<Name>Another product</Name>
<Description>bar</Description>
</Product>
</Products>
</Catalog>
将创建所需的树视图
<html>
<body>
<div>
<ul>
<li>
<span>Catalog</span>
<div>
<ul>
<li>
<span>CatalogName</span>
<div>
<span>First catalog</span>
</div>
</li>
<li>
<span>Products</span>
<div>
<ul>
<li>
<span>Product</span>
<div>
<ul>
<li>
<span>id</span>
<div>
<span>p1</span>
</div>
</li>
<li>
<span>Name</span>
<div>
<span>Some product</span>
</div>
</li>
<li>
<span>Description</span>
<div>
<span>foo</span>
</div>
</li>
</ul>
</div>
</li>
<li>
<span>Product</span>
<div>
<ul>
<li>
<span>id</span>
<div>
<span>p2</span>
</div>
</li>
<li>
<span>Name</span>
<div>
<span>Another product</span>
</div>
</li>
<li>
<span>Description</span>
<div>
<span>bar</span>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
</body>
</html>
问题是输入 XMl 的结构已更改。实际上,现在它看起来像是包装在某个根节点中的 XML 文档的集合。请参见下面的示例:
<Catalogs xmlns="somenamespace">
<Catalog>
<CatalogName>First catalog</CatalogName>
<Products>
<Product id="p1.1">
<Name>Some product</Name>
<Description>foo</Description>
</Product>
<Product id="p1.2">
<Name>Another product</Name>
<Description>bar</Description>
</Product>
</Products>
</Catalog>
<Catalog>
<CatalogName>Second catalog</CatalogName>
<Products>
<Product id="p2.1">
<Name>Some product</Name>
<Description>hoho</Description>
</Product>
<Product id="p2.2">
<Name>Strange product</Name>
<Description>bar</Description>
</Product>
</Products>
</Catalog>
...
</Catalogs>
要求是输出应显示为“合并”集合的树,其中所有不同的值都用逗号连接。查看所需的输出:
<html>
<body>
<div>
<ul>
<li>
<span>Catalog</span>
<div>
<ul>
<li>
<span>CatalogName</span>
<div>
<span>First catalog, Second catalog</span>
</div>
</li>
<li>
<span>Products</span>
<div>
<ul>
<li>
<span>Product</span>
<div>
<ul>
<li>
<span>id</span>
<div>
<span>p1.1, p2.1</span>
</div>
</li>
<li>
<span>Name</span>
<div>
<span>Some product</span>
</div>
</li>
<li>
<span>Description</span>
<div>
<span>foo, hoho</span>
</div>
</li>
</ul>
</div>
</li>
<li>
<span>Product</span>
<div>
<ul>
<li>
<span>id</span>
<div>
<span>p1.2, p2.2</span>
</div>
</li>
<li>
<span>Name</span>
<div>
<span>Another product, Strange product</span>
</div>
</li>
<li>
<span>Description</span>
<div>
<span>bar</span>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
</body>
</html>
请注意,XSLT 仍然必须是“无模式的”。 我被困在这个问题上。我看到的唯一一种解决方案是使用msxsl:script。但这是不可取的,因为在这种情况下,XSLT 可能无法在我的应用程序之外工作。
有任何想法吗?