如何在xslt中过滤

时间:2013-02-26 13:52:19

标签: xml xslt xpath xslt-2.0

我有:

<ExtData>
<table>
    <Column biz="Bus" desc="" id="Bus" >
        <CoreColumn coreEntityName="ser" coreId="Bus" coreColumnDataType="TypeInt" coreColumnLength=""/>
    </Column>
    <Column biz="ser" desc="" id="NAME" >
        <CoreColumn coreEntityName="ser" coreId="NAME" coreColumnDataType="TypeVarChar" />
    </Column>
    <Column biz="ID" desc=""  id="GLOBAL_ID" >
        <CoreColumn coreEntityName="ser" coreId="UCMDB_ID" coreColumnDataType="VarChar" />
        <CoreColumn coreEntityName="ser_xxx" coreId="UCMDB_ID" coreColumnDataType="VarChar" />
    </Column>
    <Column biz="State" desc="" id="ser_STATE" >
        <CoreColumn coreEntityName="ser" coreId="ser_STATE" coreColumnDataType="VarChar" />
    </Column>
</table>

并且需要输出&gt;&gt;&gt;由属性@coreEntityName="ser"过滤:

<ExtData>
<table>
    <Column biz="Bus" desc="" id="Bus" >
        <CoreColumn coreEntityName="ser" coreId="Bus" coreColumnDataType="TypeInt" coreColumnLength=""/>
    </Column>
    <Column biz="ser" desc="" id="NAME" >
        <CoreColumn coreEntityName="ser" coreId="NAME" coreColumnDataType="TypeVarChar" />
    </Column>
    <Column biz="ID" desc=""  id="GLOBAL_ID" >
        <CoreColumn coreEntityName="ser" coreId="UCMDB_ID" coreColumnDataType="VarChar" />
    </Column>
    <Column biz="State" desc="" id="ser_STATE" >
        <CoreColumn coreEntityName="ser" coreId="ser_STATE" coreColumnDataType="VarChar" />
    </Column>
</table>

1 个答案:

答案 0 :(得分:3)

以下XSLT样式表实现了所需的输出:

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

    <!-- Identity template : copy attributes and elements by default -->
    <xsl:template match="@*|*">
        <xsl:copy>
            <xsl:apply-templates select="@*|*" />
        </xsl:copy>
    </xsl:template>

    <!-- Exclude all the CoreColumn elements such as their @coreEntityname
         attribute is other than ser -->
    <xsl:template match="CoreColumn[@coreEntityName != 'ser']" />

</xsl:stylesheet>