使用XSLT对XML元素进行排序并为一组元素排序数据

时间:2012-10-01 07:57:20

标签: xml xslt

我有以下示例xml数据。我需要在多个级别对元素和数据进行排序。 1.必须在<InventoryRecord><SalesInRecord><SellOutRecord>之下的根级别下进行排序 2.必须对每个<InventoryRecord><SalesInRecord><SellOutRecord>字段名称中的所有元素进行排序 3.必须在每个记录集的<LOCATION_ID><LOCATION_ID_DB><LOCATION_NAME>字段中的数据上进行排序,而不管记录集类型如何。    这里重要的是,对于某些记录<LOCATION_ID>元素可能不可用。    在这种情况下,排序应仅在<LOCATION_ID_DB><LOCATION_NAME>值上进行。

<root>
<SellOutRecord>
    <FILE_TYPE>STOSO</FILE_TYPE>
    <RECORD_TYPE>VALID</RECORD_TYPE>
    <RECORD_NO>1</RECORD_NO>
    <LOCATION_ID>2-3LG-2456</LOCATION_ID>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <LOCATION_ID_DB>2-3LG-2456</LOCATION_ID_DB>
</SellOutRecord>
<SellOutRecord>
    <FILE_TYPE>STOSO</FILE_TYPE>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
    <RECORD_NO>2</RECORD_NO>
    <LOCATION_NAME>XYZ ABC</LOCATION_NAME>
    <LOCATION_ID_DB>-1</LOCATION_ID_DB>
</SellOutRecord>
<SellOutRecord>
    <FILE_TYPE>STOSO</FILE_TYPE>
    <RECORD_TYPE>WARNING</RECORD_TYPE>
    <RECORD_NO>3</RECORD_NO>
    <LOCATION_ID>2-3LG-2450</LOCATION_ID>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <LOCATION_ID_DB>-1</LOCATION_ID_DB>
</SellOutRecord>
<SellOutRecord>
    <FILE_TYPE>STOSO</FILE_TYPE>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
    <RECORD_NO>4</RECORD_NO>
    <LOCATION_ID>2-3LG-2456</LOCATION_ID>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <LOCATION_ID_DB>2-3LG-2456</LOCATION_ID_DB>
</SellOutRecord>
<SellOutRecord>
    <FILE_TYPE>STOSO</FILE_TYPE>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
    <RECORD_NO>6</RECORD_NO>
    <LOCATION_NAME>XYZ ABC</LOCATION_NAME>
    <LOCATION_ID_DB>-1</LOCATION_ID_DB>
</SellOutRecord>
<InventoryRecord>
    <FILE_TYPE>STOIV</FILE_TYPE>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
    <RECORD_NO>2</RECORD_NO>
    <LOCATION_ID>2-3LG-2450</LOCATION_ID>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <LOCATION_ID_DB>-1</LOCATION_ID_DB>
</InventoryRecord>
<InventoryRecord>
    <FILE_TYPE>STOIV</FILE_TYPE>
    <RECORD_TYPE>VALID</RECORD_TYPE>
    <RECORD_NO>4</RECORD_NO>
    <LOCATION_ID>2-3LG-2456</LOCATION_ID>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <LOCATION_ID_DB>2-3LG-2456</LOCATION_ID_DB>
</InventoryRecord>
<InventoryRecord>
    <FILE_TYPE>STOIV</FILE_TYPE>
    <RECORD_TYPE>VALID</RECORD_TYPE>
    <RECORD_NO>5</RECORD_NO>
    <LOCATION_NAME>XYZ ABC</LOCATION_NAME>>
    <LOCATION_ID_DB>-1</LOCATION_ID_DB>
</InventoryRecord>
<InventoryRecord>
    <FILE_TYPE>STOIV</FILE_TYPE>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
    <RECORD_NO>8</RECORD_NO>
    <LOCATION_ID>2-3LG-2456</LOCATION_ID>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <LOCATION_ID_DB>2-3LG-2456</LOCATION_ID_DB>
</InventoryRecord>
<SalesInRecord>
    <FILE_TYPE>STOSI</FILE_TYPE>
    <RECORD_TYPE>VALID</RECORD_TYPE>
    <RECORD_NO>3</RECORD_NO>
    <LOCATION_ID>2-3LG-2456</LOCATION_ID>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <LOCATION_ID_DB>2-3LG-2456</LOCATION_ID_DB>
</SalesInRecord>
<SalesInRecord>
    <FILE_TYPE>STOSI</FILE_TYPE>
    <RECORD_TYPE>WARNING</RECORD_TYPE>
    <RECORD_NO>3</RECORD_NO>
    <LOCATION_ID>2-3LG-2456</LOCATION_ID>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <LOCATION_ID_DB>2-3LG-2456</LOCATION_ID_DB>
</SalesInRecord>
<SalesInRecord>
    <FILE_TYPE>STOSI</FILE_TYPE>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
    <RECORD_NO>4</RECORD_NO>
    <LOCATION_ID>2-3LG-2450</LOCATION_ID>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <LOCATION_ID_DB>-1</LOCATION_ID_DB>
</SalesInRecord>
<SalesInRecord>
    <FILE_TYPE>STOSI</FILE_TYPE>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
    <RECORD_NO>7</RECORD_NO>
    <LOCATION_NAME>XYZ ABC</LOCATION_NAME>
    <LOCATION_ID_DB>-1</LOCATION_ID_DB>
</SalesInRecord>
</root>

预期的输出是。

<root>
<InventoryRecord>
    <FILE_TYPE>STOIV</FILE_TYPE>
    <LOCATION_ID_DB>-1</LOCATION_ID_DB>
    <LOCATION_NAME>XYZ ABC</LOCATION_NAME>
    <RECORD_NO>5</RECORD_NO>
    <RECORD_TYPE>VALID</RECORD_TYPE>
</InventoryRecord>
<InventoryRecord>
    <FILE_TYPE>STOIV</FILE_TYPE>
    <LOCATION_ID>2-3LG-2450</LOCATION_ID>
    <LOCATION_ID_DB>-1</LOCATION_ID_DB>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <RECORD_NO>2</RECORD_NO>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
</InventoryRecord>
<InventoryRecord>
    <FILE_TYPE>STOIV</FILE_TYPE>
    <LOCATION_ID>2-3LG-2456</LOCATION_ID>
    <LOCATION_ID_DB>2-3LG-2456</LOCATION_ID_DB>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <RECORD_NO>4</RECORD_NO>
    <RECORD_TYPE>VALID</RECORD_TYPE>
</InventoryRecord>
<InventoryRecord>
    <FILE_TYPE>STOIV</FILE_TYPE>
    <LOCATION_ID>2-3LG-2456</LOCATION_ID>
    <LOCATION_ID_DB>2-3LG-2456</LOCATION_ID_DB>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <RECORD_NO>8</RECORD_NO>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
</InventoryRecord>
<SalesInRecord>
    <FILE_TYPE>STOSI</FILE_TYPE>
    <LOCATION_ID_DB>-1</LOCATION_ID_DB>
    <LOCATION_NAME>XYZ ABC</LOCATION_NAME>
    <RECORD_NO>7</RECORD_NO>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
</SalesInRecord>
<SalesInRecord>
    <FILE_TYPE>STOSI</FILE_TYPE>
    <LOCATION_ID>2-3LG-2450</LOCATION_ID>
    <LOCATION_ID_DB>-1</LOCATION_ID_DB>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <RECORD_NO>4</RECORD_NO>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
</SalesInRecord>
<SalesInRecord>
    <FILE_TYPE>STOSI</FILE_TYPE>
    <LOCATION_ID>2-3LG-2456</LOCATION_ID>
    <LOCATION_ID_DB>2-3LG-2456</LOCATION_ID_DB>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <RECORD_NO>3</RECORD_NO>
    <RECORD_TYPE>VALID</RECORD_TYPE>
</SalesInRecord>
<SalesInRecord>
    <FILE_TYPE>STOSI</FILE_TYPE>
    <LOCATION_ID>2-3LG-2456</LOCATION_ID>
    <LOCATION_ID_DB>2-3LG-2456</LOCATION_ID_DB>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <RECORD_NO>3</RECORD_NO>
    <RECORD_TYPE>WARNING</RECORD_TYPE>
</SalesInRecord>
<SellOutRecord>
    <FILE_TYPE>STOSO</FILE_TYPE>
    <LOCATION_ID_DB>-1</LOCATION_ID_DB>
    <LOCATION_NAME>XYZ ABC</LOCATION_NAME>
    <RECORD_NO>2</RECORD_NO>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
</SellOutRecord>
<SellOutRecord>
    <FILE_TYPE>STOSO</FILE_TYPE>
    <LOCATION_ID_DB>-1</LOCATION_ID_DB>
    <LOCATION_NAME>XYZ ABC</LOCATION_NAME>
    <RECORD_NO>6</RECORD_NO>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
</SellOutRecord>
<SellOutRecord>
    <FILE_TYPE>STOSO</FILE_TYPE>
    <LOCATION_ID>2-3LG-2450</LOCATION_ID>
    <LOCATION_ID_DB>-1</LOCATION_ID_DB>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <RECORD_NO>3</RECORD_NO>
    <RECORD_TYPE>WARNING</RECORD_TYPE>
</SellOutRecord>
<SellOutRecord>
    <FILE_TYPE>STOSO</FILE_TYPE>
    <LOCATION_ID>2-3LG-2456</LOCATION_ID>
    <LOCATION_ID_DB>2-3LG-2456</LOCATION_ID_DB>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <RECORD_NO>1</RECORD_NO>
    <RECORD_TYPE>VALID</RECORD_TYPE>
</SellOutRecord>
<SellOutRecord>
    <FILE_TYPE>STOSO</FILE_TYPE>
    <LOCATION_ID>2-3LG-2456</LOCATION_ID>
    <LOCATION_ID_DB>2-3LG-2456</LOCATION_ID_DB>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <RECORD_NO>4</RECORD_NO>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
</SellOutRecord>
</root>

1 个答案:

答案 0 :(得分:1)

确实

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

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

<xsl:template match="root">
  <xsl:copy>
    <xsl:apply-templates select="*">
      <xsl:sort select="local-name()"/>
      <xsl:sort select="LOCATION_ID"/>
      <xsl:sort select="LOCATION_ID_DB"/>
      <xsl:sort select="LOCATION_NAME"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

<xsl:template match="SalesInRecord | SellOutRecord | InventoryRecord">
  <xsl:copy>
    <xsl:apply-templates select="*">
      <xsl:sort select="local-name()"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>


</xsl:stylesheet>

做你想做的事?我认为它会为您发布的输入生成您发布的输出。