2015-05-05 207 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。但我也需要从第一个删除重复的节点。 –