【问题标题】:XSLT to convert XML with grouping the value with the same tagsXSLT 转换 XML,将具有相同标签的值分组
【发布时间】:2012-03-25 20:49:47
【问题描述】:

我有一个 XML 显示字段值,字段值如下所示:

<data>
    <currentRow>
      <columnValue>usa</columnValue>
      <columnValue>ma</columnValue>
      <columnValue>boston</columnValue>
      <columnValue>bob</columnValue>
    </currentRow>
    <currentRow>
      <columnValue>usa</columnValue>
      <columnValue>ma</columnValue>
      <columnValue>boston</columnValue>
      <columnValue>george</columnValue>
    </currentRow>
    <currentRow>
      <columnValue>usa</columnValue>
      <columnValue>ny</columnValue>
      <columnValue>nyc</columnValue>
      <columnValue>mary</columnValue>
    </currentRow>
  </data>

我想生成一个看起来像

的 Xml 生成器
<Class>
  <Student>
    <Country>usa</Country>
    <State>ma</State>
    <City>boston</City>
       <name>bob</name>
       <name>george</name>
  </Student>
  <Student>
    <Country>usa</Country>
    <State>ny</State>
    <City>nyc</City>
       <name>mary</name>
  </Student>
<Class>

简而言之,我有两个问题:

  1. 我想循环当前行以生成通用 XMl
  2. 我想将学生姓名按他们居住的位置进行分组,首先是城市,然后是州,然后是国家

任何想法我该怎么做?

【问题讨论】:

  • 您的输出 XML 无效。 &lt;City&gt;boston&lt;City&gt;? &lt;name&gt;...&lt;/name&gt; 的缩进是否表明它应该在 &lt;City&gt; 元素内?请修正输出 XML 以显示您真正想要完成的任务。

标签: xml xslt xslt-1.0


【解决方案1】:

以下样式表使用 Muenchian Method 使用前 3 个 columnValue 元素的值对 currentRow 元素进行分组。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes"/>

    <!--Create a key grouping on the concatenated values of 
        country, state, and city separated by '-'-->
    <xsl:key name="students-by-country-state-city" 
             match="currentRow"
             use="concat(columnValue[1], 
                         '-', 
                         columnValue[2], 
                         '-', 
                         columnValue[3])"/>

    <xsl:template match="data">
        <Class>
            <!--apply templates to the first item in each grouping of items -->
            <xsl:apply-templates
                select="currentRow[generate-id() =
                                   generate-id(
                                     key('students-by-country-state-city', 
                                         concat(columnValue[1],
                                         '-', 
                                         columnValue[2],  
                                         '-', 
                                         columnValue[3]))[1]
                                   )]"
            />
        </Class>
    </xsl:template>

    <xsl:template match="currentRow">
        <Student>
            <Country>
                <xsl:value-of select="columnValue[1]"/>
            </Country>
            <State>
                <xsl:value-of select="columnValue[2]"/>
            </State>
            <City>
                <xsl:value-of select="columnValue[3]"/>
            </City>

            <!-- find all of the names for this grouping -->
            <xsl:for-each
                select="key('students-by-country-state-city', 
                                    concat(columnValue[1], 
                                           '-', 
                                           columnValue[2],  
                                           '-', 
                                           columnValue[3]))/columnValue[4]">
                <name>
                    <xsl:value-of select="."/>
                </name>
            </xsl:for-each>
        </Student>
    </xsl:template>

</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    相关资源
    最近更新 更多