如何使用xslt根据每条记录中的另一个元素值获取元素值

时间:2012-11-13 01:54:34

标签: xml xslt tibco

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

如果RECORD_TYPE ='警告',那么我必须打印警告下所有记录的所有ERROR_DESC值:ERROR_DESC 如果RECORD_TYPE ='ERROR',那么我必须打印所有记录下的所有ERROR_DESC值 错误:ERROR_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:
<ERROR>record 1:location id is invalid</ERROR>
<ERROR>record 2:SO:invoiced net amount is invalid</ERROR>
<ERROR>record 3:SO:transaction currency is invalid</ERROR>
<ERROR>record 3:SI:transaction document id is invalid</ERROR>
<ERROR>record 5:SI:transaction currency is null or invalid</ERROR>
<ERROR>record 6:product id is invalid</ERROR>
<ERROR>record 6:SO:invoiced net amount is invalid</ERROR>

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

1 个答案:

答案 0 :(得分:1)

此转化:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
     <xsl:apply-templates select="*[RECORD_TYPE = 'ERROR']"/>
     <xsl:apply-templates select="*[RECORD_TYPE = 'WARNING']"/>
 </xsl:template>

 <xsl:template match="root/*">
  <xsl:if test="position() = 1">
   <xsl:value-of select="concat('&#xA;', '&#xA;', RECORD_TYPE, 'S:', '&#xA;')"/>
  </xsl:if>
  <ERROR><xsl:value-of select="ERROR_DESC"/></ERROR>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档时:

<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:
<ERROR>record 1:location id is invalid</ERROR>
<ERROR>record 2:SO:invoiced net amount is invalid</ERROR>
<ERROR>record 3:SO:transaction currency is invalid</ERROR>
<ERROR>record 3:SI:transaction document id is invalid</ERROR>
<ERROR>record 5:SI:transaction currency is null or invalid</ERROR>
<ERROR>record 6:product id is invalid</ERROR>
<ERROR>record 6:SO:invoiced net amount is invalid</ERROR>

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