0

我有以下示例 xml 数据。我需要在多个级别对元素和数据进行排序。1. 排序必须在根级别的下方进行,例如2. 必须对每个或字段名称<InventoryRecord><SalesInRecord><SellOutRecord> 中的所有元素进行排序 3. 必须对每个记录集中的字段中的数据进行排序,无论记录集类型。这里重要的是,对于某些记录元素可能不可用。在这种情况下,排序应该只发生在值上。<InventoryRecord><SalesInRecord><SellOutRecord><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>
4

1 回答 1

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>

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

于 2012-10-01T09:31:42.080 回答