2013-06-12 41 views
0

刪除重複我的XML看起來像以下:XSLT,同時concating

<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<BATCHES> 
    <item> 
     <Material>1000000079</Material> 
     <Description>330 Bulk</Description> 
     <Tank>T123</Tank> 
     <Batch>2013225287</Batch> 
    </item> 
    <item> 
     <Material>1000000079</Material> 
     <Description>330 Bulk</Description> 
     <Tank>T123</Tank> 
     <Batch>2013225301</Batch> 
    </item> 
    <item> 
     <Material>1000000196</Material> 
     <Description>340R Bulk</Description> 
     <Tank>T700</Tank> 
     <Batch>1000188378</Batch> 
    </item> 
    <item> 
     <Material>1000002754</Material> 
     <Description>43 Bulk</Description> 
     <Tank>T515</Tank> 
     <Batch>2013180125</Batch> 
    </item> 
    <item> 
     <Material>1000002754</Material> 
     <Description>43 Bulk</Description> 
     <Tank>T515</Tank> 
     <Batch>2013203124</Batch> 
    </item> 
    <item> 
     <Material>1000002754</Material> 
     <Description>43 Bulk</Description> 
     <Tank>T515</Tank> 
     <Batch>2013214839</Batch> 
    </item> 
    <item> 
     <Material>1000002754</Material> 
     <Description>OGA 72043 Bulk</Description> 
     <Tank>T517</Tank> 
     <Batch>2013214342</Batch> 
    </item> 
</BATCHES> 

我的XSLT看起來像follwoing:

<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output encoding="UTF-8" indent="yes" method="xml" version="1.0"/> 
    <xsl:template match="/"> 
     <Rowsets> 
      <Rowset> 
       <xsl:variable name="materials" select=".//item[Tank!='RECV' and Tank!='PARK'] "/> 
       <xsl:for-each select="$materials"> 
        <xsl:if test="generate-id(.)=      generate-id($materials[Material=current()/Material])"> 
         <Row> 
          <Material> 
           <xsl:value-of select="Material"/> 
          </Material> 
          <Description> 
           <xsl:value-of select="Description"/> 
          </Description> 
          <Value> 
           <xsl:for-each select="$materials[Material=current()/Material]/Tank"> 
            <xsl:if test="node()"> 
             <xsl:value-of select="concat(.,'||')"/> 
            </xsl:if> 
           </xsl:for-each> 
          </Value> 
         </Row> 
        </xsl:if> 
       </xsl:for-each> 
      </Rowset> 
     </Rowsets> 
    </xsl:template> 
</xsl:stylesheet> 

XSLT的結果是:

<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<Rowsets> 
    <Rowset> 
     <Row> 
      <Material>1000000079</Material> 
      <Description>330 Bulk</Description> 
      <Value>T123||T123||</Value> 
     </Row> 
     <Row> 
      <Material>1000000196</Material> 
      <Description>340R Bulk</Description> 
      <Value>T700||</Value> 
     </Row> 
     <Row> 
      <Material>1000002754</Material> 
      <Description>43 Bulk</Description> 
      <Value>T515||T515||T515||T517||</Value> 
     </Row> 
    </Rowset> 
</Rowsets> 

我的問題是在現場,我不想連接坦克,如果它重複。

樣品應該是:

<Row> 
       <Material>1000002754</Material> 
       <Description>43 Bulk</Description> 
       <Value>T515||T517||</Value> 
      </Row> 

我嘗試各種組合,但未能實現。任何人都可以幫助嗎?

+0

您是否故意丟棄最後一項中的替代描述? (「OGA 72043批量」) – adhocgeek

回答

1

如果XML越來越大,基於密鑰的解決方案(使用xslt-1.0)會更好。

但是,大多數情況下,解決方案只有獨特的坦克價值。您可以添加兩個模板:

<xsl:template match="item" mode="tank" /> 
<xsl:template match="item[not(Material = preceding::item/Material and Tank = preceding::item/Tank)]" mode="tank" > 
    <xsl:value-of select="Tank"/> 
    <xsl:text>||</xsl:text> 
</xsl:template> 

這會忽略任何具有相同材質和相同坦克值的項目。

而且在更換<Value>應用此模板的for-each循環:

<xsl:apply-templates select="$materials[Material=current()/Material ]" mode="tank" /> 

因此試試這個:

<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output encoding="UTF-8" indent="yes" method="xml" version="1.0"/> 
    <xsl:template match="/"> 
     <Rowsets> 
      <Rowset> 
       <xsl:variable name="materials" select=".//item[Tank!='RECV' and Tank!='PARK'] "/> 
       <xsl:for-each select="$materials"> 
        <xsl:if test="generate-id(.)= generate-id($materials[Material=current()/Material])"> 
         <Row> 
          <Material> 
           <xsl:value-of select="Material"/> 
          </Material> 
          <Description> 
           <xsl:value-of select="Description"/> 
          </Description> 
          <Value> 
           <xsl:apply-templates select="$materials[Material=current()/Material ]" mode="tank" /> 
          </Value> 
         </Row> 
        </xsl:if> 
       </xsl:for-each> 
      </Rowset> 
     </Rowsets> 
    </xsl:template> 
    <xsl:template match="item" mode="tank" /> 
    <xsl:template match="item[not(Material = preceding::item/Material and Tank = preceding::item/Tank)]" mode="tank" > 
     <xsl:value-of select="Tank"/> 
     <xsl:text>||</xsl:text> 
    </xsl:template> 

</xsl:stylesheet> 

這將產生以下輸出:

<Rowsets> 
    <Rowset> 
    <Row> 
     <Material>1000000079</Material> 
     <Description>330 Bulk</Description> 
     <Value>T123||</Value> 
    </Row> 
    <Row> 
     <Material>1000000196</Material> 
     <Description>340R Bulk</Description> 
     <Value>T700||</Value> 
    </Row> 
    <Row> 
     <Material>1000002754</Material> 
     <Description>43 Bulk</Description> 
     <Value>T515||T517||</Value> 
    </Row> 
    </Rowset> 
</Rowsets> 
+0

我高舉重新發布與其他信息的問題。請看一看。 –

1

For XSLT 1.0,你想使用一種叫Muenchian grouping的東西。我做了你可能要保持在可替代的描述過於假設和簡化您的XSLT這樣的:

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

    <xsl:template match="/"> 
    <xsl:variable name="materials" select="BATCHES/item[Tank!='RECV' and Tank!='PARK']"/> 

    <Rowsets> 
     <Rowset> 
     <xsl:for-each select="$materials[not(Material=preceding-sibling::item/Material)]"> 
      <Row> 
      <xsl:variable name="items" select="//item[Material=current()/Material]"/> 
      <xsl:variable name="distinct-descriptions" select="$items[not(Description=preceding-sibling::item/Description)]"/> 
      <xsl:variable name="distinct-tanks" select="$items[not(Tank=preceding-sibling::item/Tank)]"/> 

      <Material><xsl:value-of select="Material"/></Material> 
      <Description> 
       <xsl:for-each select="$distinct-descriptions"> 
        <xsl:if test="position() != 1">||</xsl:if> 
        <xsl:value-of select="Description"/> 
       </xsl:for-each> 
      </Description> 
      <Value> 
       <xsl:for-each select="$distinct-tanks"> 
       <xsl:if test="position() != 1">||</xsl:if> 
       <xsl:value-of select="Tank"/> 
       </xsl:for-each> 
      </Value> 
      </Row> 
     </xsl:for-each> 
     </Rowset> 
    </Rowsets> 
    </xsl:template> 
</xsl:stylesheet> 

這是否做這樣的事情,你想要什麼你?