dongyun234854 2013-12-18 14:29 采纳率: 0%
浏览 132
已采纳

如何使用xsl按属性值对xml文件夹和文件进行排序?

First of all, I'm noob when it comes to xsl. I have one .xml document that is not in proper order.

xml example:

<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>

And I need this:

<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>

Whatever I try to do, I mess things up and can't even display the structure. I've tried using xsl:for-each and xsl:sort but it didn't help. Probably because I'm not doing it right. I don't know what else to do..

Here is the xsl that is already there. And changing it in anyway causes me trouble..

<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" is "some-file" in my example....

  • 写回答

1条回答 默认 最新

  • doujing5937 2013-12-18 15:33
    关注

    You just need to add a xsl:sort instruction to the famous identity transform template:

    <?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>
    

    Note that your XML example is not a well-formed document: you are missing a root element.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog