2015-05-05 200 views
0

我在寫一個XSLT來刪除多餘的roes和重複的節點有點困難。所以需要你的幫助。XSLT刪除重複的節點和額外的<Row>

我的XML看起來如下:

<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<Rowsets CachedTime="" DateCreated="2015-05-05T19:27:06" EndDate="2015-05-05T19:27:06" StartDate="2015-05-05T18:27:06" Version="14.0.0 Build(802)"> 
    <Rowset> 
     <Columns> 
      <Column Description="DateTime" MaxRange="0" MinRange="0" Name="DateTime" SQLDataType="93" SourceColumn="DateTime"/> 
      <Column Description="" MaxRange="0" MinRange="0" Name="_10LI132.PV" SQLDataType="6" SourceColumn="10LI132.PV"/> 
      <Column Description="" MaxRange="0" MinRange="0" Name="_10LQ132.PV" SQLDataType="6" SourceColumn="10LQ132.PV"/> 
      <Column Description="" MaxRange="0" MinRange="0" Name="_10TI112.PV" SQLDataType="6" SourceColumn="10TI112.PV"/> 
      <Column Description="" MaxRange="0" MinRange="0" Name="_10LI135.PV" SQLDataType="6" SourceColumn="10LI135.PV"/> 
      <Column Description="" MaxRange="0" MinRange="0" Name="_10LQ132.PV" SQLDataType="6" SourceColumn="10LQ132.PV"/> 
      <Column Description="" MaxRange="0" MinRange="0" Name="_10LI127.PV" SQLDataType="6" SourceColumn="10LI127.PV"/> 
      <Column Description="" MaxRange="0" MinRange="0" Name="_10TI112.PV" SQLDataType="6" SourceColumn="10TI112.PV"/> 
      <Column Description="" MaxRange="0" MinRange="0" Name="_10LQ127.PV" SQLDataType="6" SourceColumn="10LQ127.PV"/> 
     </Columns> 
     <Row> 
      <DateTime>2015-05-05T18:27:06</DateTime> 
      <A>55465.359375</A> 
      <B>1808040</B> 
      <C>-331.424926757812</C> 
      <D>-74553.75</D> 
      <B>1808040</B> 
      <F>-10100.994140625</F> 
      <C>-331.424926757812</C> 
      <G>-445363.5625</G> 
     </Row> 
     <Row> 
      <DateTime>2015-05-05T18:27:06</DateTime> 
      <A>NA</A> 
      <B>NA</B> 
      <C>NA</C> 
      <D>NA</D> 
      <B>1808040</B> 
      <F>NA</F> 
      <C>NA</C> 
      <G>NA</G> 
     </Row> 
     <Row> 
      <DateTime>2015-05-05T18:27:06</DateTime> 
      <A>NA</A> 
      <B>NA</B> 
      <C>NA</C> 
      <D>NA</D> 
      <B>NA</B> 
      <F>NA</F> 
      <C>-331.424926757812</C> 
      <G>NA</G> 
     </Row> 
    </Rowset> 
</Rowsets> 

我想我的生成的XML如下:

<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<Rowsets CachedTime="" DateCreated="2015-05-05T19:27:06" EndDate="2015-05-05T19:27:06" StartDate="2015-05-05T18:27:06" Version="14.0.0 Build(802)"> 
    <Rowset> 
     <Columns> 
      <Column Description="DateTime" MaxRange="0" MinRange="0" Name="DateTime" SQLDataType="93" SourceColumn="DateTime"/> 
      <Column Description="" MaxRange="0" MinRange="0" Name="_10LI132.PV" SQLDataType="6" SourceColumn="10LI132.PV"/> 
      <Column Description="" MaxRange="0" MinRange="0" Name="_10LQ132.PV" SQLDataType="6" SourceColumn="10LQ132.PV"/> 
      <Column Description="" MaxRange="0" MinRange="0" Name="_10TI112.PV" SQLDataType="6" SourceColumn="10TI112.PV"/> 
      <Column Description="" MaxRange="0" MinRange="0" Name="_10LI135.PV" SQLDataType="6" SourceColumn="10LI135.PV"/> 
      <Column Description="" MaxRange="0" MinRange="0" Name="_10LQ132.PV" SQLDataType="6" SourceColumn="10LQ132.PV"/> 
      <Column Description="" MaxRange="0" MinRange="0" Name="_10LI127.PV" SQLDataType="6" SourceColumn="10LI127.PV"/> 
      <Column Description="" MaxRange="0" MinRange="0" Name="_10TI112.PV" SQLDataType="6" SourceColumn="10TI112.PV"/> 
      <Column Description="" MaxRange="0" MinRange="0" Name="_10LQ127.PV" SQLDataType="6" SourceColumn="10LQ127.PV"/> 
     </Columns> 
     <Row> 
      <DateTime>2015-05-05T18:27:06</DateTime> 
      <A>55465.359375</A> 
      <B>1808040</B> 
      <C>-331.424926757812</C> 
      <D>-74553.75</D> 
      <F>-10100.994140625</F> 
      <G>-445363.5625</G> 
     </Row> 
    </Rowset> 
</Rowsets> 

請注意,節點,ETS是動態生成的所以不能在XSLT硬編碼他們。

讓我知道你是否有任何想法來解決我的問題。

在此先感謝。

+1

你到目前爲止嘗試了什麼?刪除這些行的標準是什麼? – leu

+0

我只需要保持第一個。所有其他 s需要被刪除。 –

回答

1

你使用xslt 2.0嗎?如果是這樣,請參閱下面的樣式表:

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

    <xsl:strip-space elements="*"/> 
    <xsl:output method="xml" indent="yes"/> 

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

    <xsl:template match="Rowset/Row[1]"> 
     <xsl:copy> 
      <xsl:for-each-group select="*" group-by="."> 
       <xsl:copy-of select="current-group()[1]"/> 
      </xsl:for-each-group> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="//Row[preceding-sibling::Row]"/> 

</xsl:stylesheet> 
2

這個問題的一個簡單的解決方案是查看之前的,並查看前面有一個名稱相同的元素。隨着那些 - 什麼也不做:

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

    <xsl:template match="//Row[preceding-sibling::Row]"/> 

    <xsl:template match="//Row/*[name() = preceding-sibling::*/name()]"/> 

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

所有其他元素被照原樣複製。

+0

謝謝Leu。這將刪除所有後續的 s。但我也需要從第一個刪除重複的節點。 –