1

我需要根据每条记录中的另一个元素值获取一个元素值。

如果 RECORD_TYPE='ERROR' 那么我必须为 ERRORS:ERROR_DESC 下的所有记录打印所有不同(唯一)的 ERROR_DESC 值

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

<root>
<SellOutRecord>
        <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>
    </SellOutRecord>
    <SalesInRecord>
        <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>
    </SalesInRecord>
    <SellOutRecord>
        <RECORD_TYPE>ERROR</RECORD_TYPE>
        <ERROR_DESC>record 2:location id 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>
    </SellOutRecord>
    <SellOutRecord>
        <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>
    </SellOutRecord>
    <SalesInRecord>
        <RECORD_TYPE>ERROR</RECORD_TYPE>
        <ERROR_DESC>record 2:location 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>
    </SalesInRecord>
    <SalesInRecord>
        <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>
    </SalesInRecord>
    <SalesInRecord>
        <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>
    </SalesInRecord>
    <InventoryRecord>
        <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>
    </InventoryRecord>
    <SellOutRecord>
        <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>
</SellOutRecord>
    <SellOutRecord>
        <RECORD_TYPE>VALID</RECORD_TYPE>
        <RECORD_NO>7</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>
</SellOutRecord>
    <SellInRecord>
        <RECORD_TYPE>VALID</RECORD_TYPE>
        <RECORD_NO>7</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>
</SellInRecord>
</root>

预期输出为:

<root>
<ERROR>record 1:location id is invalid</ERROR>
<ERROR>record 2:location id 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>
</root>

提前致谢。

4

2 回答 2

1

我相信这应该这样做:

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

  <!-- A key for the ERROR_DESC of all items that have an error -->
  <xsl:key name="error" match="root/*[RECORD_TYPE = 'ERROR']/ERROR_DESC" use="."/>

  <xsl:template match="root">
    <root>
      <!-- Use Muenchian grouping to apply templates to distinct ERROR_DESCs -->
      <xsl:apply-templates
        select="*/ERROR_DESC[generate-id(.) = generate-id(key('error', .)[1])]" />
    </root>
  </xsl:template>

  <!-- A template to handle ERROR_DESCs -->
  <xsl:template match="ERROR_DESC">
    <ERROR>
      <xsl:value-of select="."/>
    </ERROR>
  </xsl:template>
</xsl:stylesheet>
于 2013-01-23T07:25:21.703 回答
1

I. 一个纯粹的“推式”XSLT 1.0 解决方案:

<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:key name="kErrDescByVal" match="*[RECORD_TYPE='ERROR']/ERROR_DESC"
          use="normalize-space()"/>

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

 <xsl:template match="*/*/*" priority="-1"/>

 <xsl:template match=
 "ERROR_DESC[generate-id() = generate-id(key('kErrDescByVal',normalize-space())[1])]">
  <ERROR><xsl:apply-templates/></ERROR>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<root>
    <SellOutRecord>
        <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>
    </SellOutRecord>
    <SalesInRecord>
        <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>
    </SalesInRecord>
    <SellOutRecord>
        <RECORD_TYPE>ERROR</RECORD_TYPE>
        <ERROR_DESC>record 2:location id 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>
    </SellOutRecord>
    <SellOutRecord>
        <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>
    </SellOutRecord>
    <SalesInRecord>
        <RECORD_TYPE>ERROR</RECORD_TYPE>
        <ERROR_DESC>record 2:location 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>
    </SalesInRecord>
    <SalesInRecord>
        <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>
    </SalesInRecord>
    <SalesInRecord>
        <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>
    </SalesInRecord>
    <InventoryRecord>
        <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>
    </InventoryRecord>
    <SellOutRecord>
        <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>
    </SellOutRecord>
    <SellOutRecord>
        <RECORD_TYPE>VALID</RECORD_TYPE>
        <RECORD_NO>7</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>
    </SellOutRecord>
    <SellInRecord>
        <RECORD_TYPE>VALID</RECORD_TYPE>
        <RECORD_NO>7</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>
    </SellInRecord>
</root>

产生了想要的正确结果:

<root>
   <ERROR>record 1:location id is invalid</ERROR>
   <ERROR>record 2:location id 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>
</root>

说明

  1. 使用Muenchian 分组方法

  2. 用于normalize-space()将两个仅在空白处不同的字符串视为相等。


二、XSLT 2.0 解决方案

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

 <xsl:key name="kErrDescByVal" match="*[RECORD_TYPE='ERROR']/ERROR_DESC"
          use="normalize-space()"/>

 <xsl:template match="/*">
     <root>
       <xsl:for-each-group select="*[RECORD_TYPE='ERROR']/ERROR_DESC"
                           group-by="normalize-space()">
       <ERROR><xsl:apply-templates/></ERROR>
     </xsl:for-each-group>
     </root>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于同一个 XML 文档(如上)时,会产生相同的正确结果

<root>
   <ERROR>record 1:location id is invalid</ERROR>
   <ERROR>record 2:location id 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>
</root>

说明

<xsl:for-each-group>使用带有group-by属性的 XSLT 2.0 指令。

于 2013-01-23T13:39:52.313 回答