【问题标题】:XSLT to remove duplicate nodes & extra <Row>XSLT 删除重复节点和额外的 <Row>
【发布时间】:2015-05-05 19:42:37
【问题描述】:

我在编写 XSLT 来删除额外的 roes 和重复节点时几乎没有遇到什么困难。所以需要你的帮助。

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

请注意,nodes 和 ets 是动态生成的,因此不能在 XSLT 中对它们进行硬编码。

如果您有任何想法可以解决我的问题,请告诉我。

提前致谢。

【问题讨论】:

  • 到目前为止你尝试了什么?删除那些 Row 的标准是什么?
  • 我只需要保留第一个 。所有其他 都需要删除。

标签: xml xslt


【解决方案1】:

这个问题的一个简单解决方案是查看前面有RowRow,并查看前面有同名的元素。有了这些 - 什么都不做:

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

所有其他元素按原样复制。

【讨论】:

  • 谢谢列。这将删除所有后续的 。但我也需要从第一个 中删除重复节点。
【解决方案2】:

您使用 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> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    相关资源
    最近更新 更多