【问题标题】:XSLT-1.0: How to sort a child node group and keep its hierarchyXSLT-1.0:如何对子节点组进行排序并保持其层次结构
【发布时间】:2017-08-09 18:24:20
【问题描述】:

我在这方面花了很多时间,并认为我会寻求可能的解决方案。这是我为测试而简化的 XML。

<Data>
    <Superbill>
        <Attributes>
        </Attributes>
        <Children>
            <ListByMatter>
                <ListByMatter>
                    <Children>
                        <ListofProfDetailTime>
                            <ListofProfDetailTime>
                                <Attributes>
                                    <id>1</id>
                                    <Date>2011-02-11</Date>
                                </Attributes>
                            </ListofProfDetailTime>
                            <ListofProfDetailTime>
                                <Attributes>
                                    <id>2</id>
                                    <Date>2010-11-02</Date>
                                </Attributes>
                            </ListofProfDetailTime>
                            <ListofProfDetailTime>
                                <Attributes>
                                    <id>11</id>
                                    <Date>2015-09-12</Date>
                                </Attributes>
                            </ListofProfDetailTime>
                            <ListofProfDetailTime>
                                <Attributes>  
                                    <id>1</id>
                                    <Date>2013-11-12</Date>
                                </Attributes>
                            </ListofProfDetailTime>
                        </ListofProfDetailTime>
                    </Children>
                </ListByMatter>
            </ListByMatter>
        </Children>
    </Superbill>
</Data>

我在下面为我的转换编写了这个,但无法弄清楚如何从我在原始层次结构中得到的有序结果中替换旧节点。我正在使用这个网站http://xsltransform.net/ 来快速进行更改,但我确定我遗漏了一些简单的东西,或者它会比预期的要复杂得多。我最近才开始自学,需要一些指导。

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
      <xsl:copy>
        <xsl:apply-templates select="//ListofProfDetailTime/ListofProfDetailTime">
          <xsl:sort select="Attributes/Date" order="ascending"/>
        </xsl:apply-templates>
      </xsl:copy>
   </xsl:template>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>

</xsl:transform>

【问题讨论】:

  • 您想要的输出 XML 看起来如何?

标签: xml sorting xslt-1.0


【解决方案1】:

不确定您的问题到底是什么。如果您尝试保留原始层次结构,并且仅更改内部 ListofProfDetailTime 元素的排序顺序,请更改以下内容:

    <xsl:template match="/">
      <xsl:copy>
        <xsl:apply-templates select="//ListofProfDetailTime/ListofProfDetailTime">
          <xsl:sort select="Attributes/Date" order="ascending"/>
        </xsl:apply-templates>
      </xsl:copy>
   </xsl:template>

到:

<xsl:template match="Children/ListofProfDetailTime">
    <xsl:copy>
        <xsl:apply-templates select="ListofProfDetailTime">
            <xsl:sort select="Attributes/Date" order="ascending"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

按照您现在的方式,您的模板与 / 根节点匹配,并从那里将模板直接应用于 ListofProfDetailTime 后代 - 这样中间级别就不会被处理。

【讨论】:

  • 这正是我需要的结果,感谢您的解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-14
  • 2022-01-08
  • 2020-12-25
  • 1970-01-01
  • 2014-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多