Unique record count for a particular node value with XSLT

雨燕双飞 提交于 2020-01-05 06:32:49

问题


How to count unique values when a particular node value is ERROR. The below is the xml source. there different data sets like SellOut, SellIn and Inventory but i want have a unique record count for the RECORD_TYPE=ERROR irrespective of data set type

<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>

the output has to be record_count=5. Thanks for your help...


回答1:


In XSLT 1.0, distincting is rather non intuitive. I've assumed the root node is xml and that what makes records unique is the RECORD_NO.

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

    <xsl:template match="/xml">
        record_count=<xsl:value-of select="count(*[RECORD_TYPE='ERROR' and not(following::*[RECORD_TYPE='ERROR']/RECORD_NO/text() = RECORD_NO/text())])" />
    </xsl:template>
</xsl:stylesheet>


来源:https://stackoverflow.com/questions/12462112/unique-record-count-for-a-particular-node-value-with-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!