首先,对于xsl,我是noob。 我有一个不正确的.xml文档。
xml示例:
<folder-one order="2">
<another-folder order="2">
<some-file order="2">title</some-file>
<some-file order="1">title</some-file>
<some-file order="3">title</some-file>
</another-folder>
<another-folder order="1">
<some-file order="2">title</some-file>
<some-file order="1">title</some-file>
<some-file order="3">title</some-file>
</another-folder>
</folder-one>
<folder-two order="1">
<another-folder order="2">
<some-file order="2">title</some-file>
<some-file order="1">title</some-file>
<some-file order="3">title</some-file>
</another-folder>
<another-folder order="1">
<some-file order="2">title</some-file>
<some-file order="1">title</some-file>
<some-file order="3">title</some-file>
</another-folder>
</folder-two>
我需要这个:
<folder-one order="1">
<another-folder order="1">
<some-file order="1">title</some-file>
<some-file order="2">title</some-file>
<some-file order="3">title</some-file>
</another-folder>
<another-folder order="2">
<some-file order="1">title</some-file>
<some-file order="2">title</some-file>
<some-file order="3">title</some-file>
</another-folder>
</folder-one>
<folder-two order="2">
<another-folder order="1">
<some-file order="1">title</some-file>
<some-file order="2">title</some-file>
<some-file order="3">title</some-file>
</another-folder>
<another-folder order="2">
<some-file order="1">title</some-file>
<some-file order="2">title</some-file>
<some-file order="3">title</some-file>
</another-folder>
</folder-two>
无论我尝试做什么,我都搞砸了,甚至无法显示结构。 我尝试过使用xsl:for-each和xsl:sort但它没有帮助。可能是因为我做得不对。我不知道还能做什么......
这是已经存在的xsl。无论如何改变它会给我带来麻烦..
<xsl:stylesheet version="1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
xmlns:ectd = "http://www.ich.org/ectd"
xmlns:xlink = "http://www.w3c.org/1999/xlink">
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" href="util/style/screen.css" type="text/css" media="screen"/>
</head>
<body>
<h2>eCTD <font size="-1"> DTD version <xsl:value-of select="/ectd:ectd/@dtd-version"/></font></h2>
<xsl:apply-templates select="/ectd:ectd/*"/>
</body>
</html>
</xsl:template>
<xsl:template match="*">
<ul type="square">
<li>
<xsl:value-of select="name()"/>
<font color="green">
<xsl:if test="@manufacturer != ''"> [manufacturer: <xsl:value-of select="@manufacturer"/>] </xsl:if>
<xsl:if test="@substance != ''"> [substance: <xsl:value-of select="@substance"/>] </xsl:if>
<xsl:if test="@product-name != ''"> [product name: <xsl:value-of select="@product-name"/>] </xsl:if>
<xsl:if test="@dosageform != ''"> [dosage form: <xsl:value-of select="@dosageform"/>] </xsl:if>
<xsl:if test="@indication != ''"> [indication: <xsl:value-of select="@indication"/>] </xsl:if>
<xsl:if test="@excipient != ''"> [excipient: <xsl:value-of select="@excipient"/>] </xsl:if>
</font>
</li>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="leaf">
<ul type="square">
<li>
<xsl:element name="a">
<xsl:attribute name="href"><xsl:value-of select="@xlink:href"/></xsl:attribute>
<xsl:value-of select="title"/>
</xsl:element>
<font color="red">
[<xsl:value-of select="@operation"/>]
</font>
</li>
</ul>
</xsl:template>
<xsl:template match="node-extension">
<ul type="square">
<li>
<xsl:value-of select="title"/>
<xsl:apply-templates select="leaf|node-extension"/>
</li>
</ul>
</xsl:template>
“leaf”在我的例子中是“some-file”....
答案 0 :(得分:1)
您只需要向着名的identity transform template添加xsl:sort
指令:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()">
<xsl:sort select="@order" data-type="number" order="ascending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
请注意,您的XML示例不是格式正确的文档:您缺少根元素。