【问题标题】:How to group flat xml into 2 levels in xslt 2.0如何在 xslt 2.0 中将平面 xml 分为 2 个级别
【发布时间】:2015-01-01 17:41:05
【问题描述】:

我有一个几乎扁平的 xml,其中有几个条目,其中包含与要在条目元数据之前显示的标题相关的信息。这些标题可以分为两级,第二级是可选的。请提出实现这一目标的方法。

输入xml:

<world>

	<country>
		<name>USA</name>
		<code>001</code>
		<category type="developed" scheme="level1"/>
		<category type="rich" scheme="level2"/>
	</country>
	<country>
		<name>UK</name>
		<code>044</code>
		<category type="developed" scheme="level1"/>
		<category type="rich" scheme="level2"/>
	</country>
	<country>
		<name>LATVIA</name>
		<code>371</code>
		<category type="developed" scheme="level1"/>
	</country>
	<country>
		<name>BHUTAN</name>
		<code>975</code>
		<category type="developing" scheme="level1"/>
	</country>

</world>

期望的输出:

<world>
  <developed>
    <rich>
      <country>
        <name>USA</name>
        <code>001</code>
      </country>
      <country>
        <name>UK</name>
        <code>044</code>
      </country>
    </rich>
    <country>
      <name>LATVIA</name>
      <code>371</code>
    </country>

  </developed>
  <developing>
    <country>
      <name>BHUTAN</name>
      <code>975</code>
    </country>
  </developing>
</world>

【问题讨论】:

    标签: xml xslt xslt-2.0 xslt-grouping


    【解决方案1】:

    嵌套两个for-each-group:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="xs"
        version="2.0">
    
    <xsl:output indent="yes"/>
    
    <xsl:template match="world">
      <xsl:copy>
        <xsl:for-each-group select="country" group-by="category[@scheme = 'level1']/@type">
          <xsl:element name="{current-grouping-key()}">
            <xsl:for-each-group select="current-group()[category[@scheme = 'level2']]" group-by="category[@scheme = 'level2']/@type">
              <xsl:element name="{current-grouping-key()}">
                <xsl:apply-templates select="current-group()"/>
              </xsl:element>
            </xsl:for-each-group>
            <xsl:apply-templates select="current-group()[not(category[@scheme = 'level2'])]"/>
          </xsl:element>
        </xsl:for-each-group>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="country">
      <xsl:copy>
        <xsl:copy-of select="* except category"/>
      </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • 非常感谢,这很明显......我缺少的是整个组的应用模板。
    猜你喜欢
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多