2

我需要根据每条记录中的另一个元素值获取一个元素值。我必须根据第二个元素值将它们分为两组。

标准:

  • 如果RECORD_TYPE='WARNING'那么我必须打印WARNINGSERROR_DESC下所有记录的所有值:。

  • 如果RECORD_TYPE='ERROR'那么我必须打印ERRORSERROR_DESC下所有记录的所有值:

  • 每个错误描述都应打印在新行中。

示例输入:

<root>
<SellOutErrorRecord>
    <RECORD_TYPE>WARNING</RECORD_TYPE>
    <ERROR_DESC>record 1:location id is invalid</ERROR_DESC>
    <RECORD_NO>1</RECORD_NO>
    <LOCATION_NAME>XYZ el</LOCATION_NAME>
    <PRODUCT_NUMBER>336549R-001</PRODUCT_NUMBER>
    <PRODUCT_OPTION>0D1</PRODUCT_OPTION>
    <PRODUCT_LINE>LA</PRODUCT_LINE>
</SellOutErrorRecord>
<SalesInErrorRecord>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
    <ERROR_DESC>record 1:location id is invalid</ERROR_DESC>
    <RECORD_NO>1</RECORD_NO>
    <LOCATION_NAME>XYZ el</LOCATION_NAME>
    <PRODUCT_NUMBER>336549R-001</PRODUCT_NUMBER>
    <PRODUCT_OPTION>0D1</PRODUCT_OPTION>
    <PRODUCT_LINE>LA</PRODUCT_LINE>
    <PRODUCT_LINE_TYPE>C</PRODUCT_LINE_TYPE>
</SalesInErrorRecord>
<SellOutErrorRecord>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
    <ERROR_DESC>record 2:SO:invoiced net amount is invalid</ERROR_DESC>
    <RECORD_NO>2</RECORD_NO>
    <LOCATION_NAME>XYZ el</LOCATION_NAME>
    <PRODUCT_NUMBER>3X-KN73C-DB</PRODUCT_NUMBER>
    <PRODUCT_OPTION>0D1</PRODUCT_OPTION>
    <PRODUCT_LINE>HA</PRODUCT_LINE>
    <PRODUCT_LINE_TYPE>E</PRODUCT_LINE_TYPE>
</SellOutErrorRecord>
<SellOutErrorRecord>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
    <ERROR_DESC>record 3:SO:transaction currency is invalid</ERROR_DESC>
    <RECORD_NO>3</RECORD_NO>
    <LOCATION_NAME>XYZ el</LOCATION_NAME>
    <PRODUCT_NUMBER>339112-B25</PRODUCT_NUMBER>
    <PRODUCT_OPTION>0D1</PRODUCT_OPTION>
    <PRODUCT_LINE>J3</PRODUCT_LINE>
    <PRODUCT_LINE_TYPE>E</PRODUCT_LINE_TYPE>
</SellOutErrorRecord>
<SalesInErrorRecord>
    <RECORD_TYPE>WARNING</RECORD_TYPE>
    <ERROR_DESC>record 3:SI:buy quantity is zero</ERROR_DESC>
    <RECORD_NO>3</RECORD_NO>
    <LOCATION_NAME>XYZ el</LOCATION_NAME>
    <PRODUCT_NUMBER>339112-B25</PRODUCT_NUMBER>
    <PRODUCT_OPTION>0D1</PRODUCT_OPTION>
    <PRODUCT_LINE>J3</PRODUCT_LINE>
    <PRODUCT_LINE_TYPE>E</PRODUCT_LINE_TYPE>
</SalesInErrorRecord>
<SalesInErrorRecord>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
    <ERROR_DESC>record 3:SI:transaction document id is invalid</ERROR_DESC>
    <RECORD_NO>3</RECORD_NO>
    <LOCATION_NAME>XYZ el</LOCATION_NAME>
    <PRODUCT_NUMBER>339112-B25</PRODUCT_NUMBER>
    <PRODUCT_OPTION>0D1</PRODUCT_OPTION>
    <PRODUCT_LINE>J3</PRODUCT_LINE>
    <PRODUCT_LINE_TYPE>E</PRODUCT_LINE_TYPE>
</SalesInErrorRecord>
<SellOutErrorRecord>
    <RECORD_TYPE>WARNING</RECORD_TYPE>
    <ERROR_DESC>record 4:SO:invoiced net amount is invalid</ERROR_DESC>
    <RECORD_NO>4</RECORD_NO>
    <LOCATION_NAME>XYZ el</LOCATION_NAME>
    <PRODUCT_NUMBER>445860-B21</PRODUCT_NUMBER>
    <PRODUCT_OPTION>0D1</PRODUCT_OPTION>
    <PRODUCT_LINE>MV</PRODUCT_LINE>
    <PRODUCT_LINE_TYPE>C</PRODUCT_LINE_TYPE>
</SellOutErrorRecord>
<SalesInErrorRecord>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
    <ERROR_DESC>record 5:SI:transaction currency is null or invalid</ERROR_DESC>
    <RECORD_NO>5</RECORD_NO>
    <LOCATION_NAME>XYZ el</LOCATION_NAME>
    <PRODUCT_NUMBER>339112-B25</PRODUCT_NUMBER>
    <PRODUCT_OPTION>0D1</PRODUCT_OPTION>
    <PRODUCT_LINE>J3</PRODUCT_LINE>
    <PRODUCT_LINE_TYPE>E</PRODUCT_LINE_TYPE>
</SalesInErrorRecord>
<InventoryErrorRecord>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
    <ERROR_DESC>record 6:product id is invalid</ERROR_DESC>
    <RECORD_NO>6</RECORD_NO>
    <LOCATION_NAME>XYZ XYZ</LOCATION_NAME>
    <PRODUCT_NUMBER>331184-B21</PRODUCT_NUMBER>
    <PRODUCT_OPTION>0S1</PRODUCT_OPTION>
    <PRODUCT_LINE>R8</PRODUCT_LINE>
    <PRODUCT_LINE_TYPE>E</PRODUCT_LINE_TYPE>
</InventoryErrorRecord>
<SellOutErrorRecord>
    <RECORD_TYPE>ERROR</RECORD_TYPE>
    <ERROR_DESC>record 6:SO:invoiced net amount is invalid</ERROR_DESC>
    <RECORD_NO>6</RECORD_NO>
    <LOCATION_NAME>XYZ el</LOCATION_NAME>
    <PRODUCT_NUMBER>339112-B25</PRODUCT_NUMBER>
    <PRODUCT_OPTION>0D1</PRODUCT_OPTION>
    <PRODUCT_LINE>J3</PRODUCT_LINE>
    <PRODUCT_LINE_TYPE>E</PRODUCT_LINE_TYPE>
</SellOutErrorRecord>
</root>

预期输出:

ERRORS:
record 1:location id is invalid
record 2:SO:invoiced net amount is invalid
record 3:SO:transaction currency is invalid

WARNINGS:
record 1:location id is invalid
record 3:SI:buy quantity is zero
record 4:SO:invoiced net amount is invalid
4

1 回答 1

1

以下样式表用于xsl:key按元素的RECORD_TYPE值对元素进行分组并生成分组的文本输出。

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

    <!--Create a key matching on each of the elements that contain RECORD_TYPE, and use it's value as the lookup key -->
    <xsl:key name="record-type" match="*[RECORD_TYPE]" use="RECORD_TYPE" />

    <xsl:template match="/">
        <!--generate output for all ERRORS -->
        <xsl:apply-templates select="key('record-type','ERROR')" />
        <!--generate a line-feed to separate the record types -->
        <xsl:text>&#xA;</xsl:text>
        <!--generate output for all WARNINGS -->
        <xsl:apply-templates select="key('record-type','WARNING')" />
    </xsl:template>

   <!--For the first of each type of record, generate a header row -->
   <xsl:template match="/*/*[generate-id()=generate-id(key('record-type',RECORD_TYPE)[1])]">
       <xsl:value-of select="RECORD_TYPE"/>
        <xsl:text>S:&#xA;</xsl:text>           
        <xsl:apply-templates select="ERROR_DESC" />
   </xsl:template>

    <!--Default processing for elements is to apply-templates to child elements-->
    <xsl:template match="*">
        <xsl:apply-templates select="*"/>
    </xsl:template>

    <!--For ERROR_DESC, output it's text value and a line-feed -->
    <xsl:template match="ERROR_DESC">
        <xsl:value-of select="."/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>

</xsl:stylesheet>

当应用于提供的 XML 时,它会产生以下输出:

ERRORS:
record 1:location id is invalid
record 2:SO:invoiced net amount is invalid
record 3:SO:transaction currency is invalid
record 3:SI:transaction document id is invalid
record 5:SI:transaction currency is null or invalid
record 6:product id is invalid
record 6:SO:invoiced net amount is invalid

WARNINGS:
record 1:location id is invalid
record 3:SI:buy quantity is zero
record 4:SO:invoiced net amount is invalid
于 2012-11-11T14:22:20.397 回答