复制树节点并删除XSLT中的子节点

时间:2014-02-07 18:39:38

标签: xml xslt subtree

我试着解释一下,我的英语很差:

我有一个XML文件:

<?xml version="1.0" encoding="utf-8"?>
<SAT_DECL>
    <SAT_CONCEPTOS>
        <SAT_GRP_0301 v="" simplificado="" acreditamientoBuenFin="">
            <SAT_DI>
                <HEADER1 v="" />
                <SAT_A2 v="0" />
                <SAT_A4 v="0" />
                <SAT_A6 v="0" />
                <SAT_A6BIS v="0" />
                <SAT_A22009 v="" />
                <SAT_A42009 v="" />
                <SAT_A8 v="0" />
                <SAT_A10 v="" />
                <HEADER7 v="" />
                <SAT_36A v="0" />
                <SAT_36B v="0" />
                <SAT_36C v="0" />
                <SAT_36D v="0" />
                <SAT_36D2009 v="0" />
                <SAT_36E2009 v="0" />
                <SAT_36 v="0" />
                <SAT_37 v="" />
                <SAT_39 v="" />
                <SAT_41 v="" />
                <SAT_43 v="" />
                <SAT_45 v="0" />
                <SAT_46 v="" />
                <SAT_47 v="" />
                <SAT_48 v="0" />
                <SAT_49 v="" />
                <SAT_50 v="" />
                <SAT_51 v="0" />
                <SAT_52 v="0" />
                <SAT_53 v="" />
                <SAT_54 v="0" />
                <HEADER8 v="" />
                <SAT_56 v="" />
                <SAT_57 v="" />
                <SAT_562009 v="" />
                <SAT_572009 v="" />
                <SAT_58 v="" />
                <SAT_59 v="" />
                <SAT_60 v="0" />
                <SAT_61 v="0" />
                <SAT_62 v="0" />
                <SAT_63 v="" />
                <SAT_64 v="" />
                <SAT_65 v="0" />
                <SAT_66 v="86576" />
                <SAT_67 v="" />
                <SAT_68 v="86576" />
                <SAT_69 v="" />
                <SAT_70 v="" />
                <SAT_71 v="0" />
                <SAT_72 v="1000" />
                <SAT_72BIS v="85576" />
                <SAT_72ABIS v="" />
                <SAT_73 v="85576" />
                <SAT_73BIS v="0" />
                <SAT_ACREDIT_VEC/>
            </SAT_DI>
        </SAT_GRP_0301>
    </SAT_CONCEPTOS>
</SAT_DECL>

然后,我创建了两个模板,它们从SAT_DI节点获取每个元素,并在每一行中创建一个具有两对名称值的表:

<xsl:template name="RENGLONESDI">
    <xsl:param name="DI"/>
    <xsl:param name="relleno"/>
    <xsl:param name="largo"/>
    <xsl:variable name="count" select="count($DI/child::*[name(.) !='SAT_OTROS_EST_VEC' and name(.) !='SAT_BONOS_CARB_VEC' and 
                                                          name(.) != 'SAT_COMPENSAC_VEC' and name(.) != 'SAT_ACREDIT_VEC' and 
                                                          name(.) != 'HEADER1' and name(.) != 'HEADER7' and
                                                          name(.) != 'HEADER8'])"/>
    <xsl:variable name="mitad" select="ceiling($count div 2)"/>
    <xsl:for-each select="$DI/child::*[name(.) !='SAT_OTROS_EST_VEC' and name(.) !='SAT_BONOS_CARB_VEC' and 
                                        name(.) != 'SAT_COMPENSAC_VEC' and name(.) != 'SAT_ACREDIT_VEC' and 
                                        name(.) != 'HEADER1' and name(.) != 'HEADER7' and
                                        name(.) != 'HEADER8']">
        <xsl:if test="position() &lt;= $mitad">
            <xsl:variable name="cve1" select="name(.)"/>
            <xsl:variable name="cve2" select="name(./following-sibling::*[$mitad])"/>
            <xsl:variable name="val1" select="./@v"/>
            <xsl:variable name="val2" select="./following-sibling::*[$mitad]/@v"/>
            <xsl:call-template name="RENGLON">
                <xsl:with-param name="col1">
                    <xsl:call-template name="CAMPOSDI">
                        <xsl:with-param name="clave" select="$cve1"/>
                        <xsl:with-param name="ejercicio" select="$gEjercicio"/>
                    </xsl:call-template>-<xsl:value-of select="$cve1"/>-<xsl:value-of select="starts-with($cve1,'HEADER')"/>
                </xsl:with-param>
                <xsl:with-param name="val1">
                    <xsl:call-template name="VALORDI">
                        <xsl:with-param name="clave" select="$cve1"/>
                        <xsl:with-param name="valor" select="$val1"/>
                        <xsl:with-param name="relleno" select="$relleno"/>
                        <xsl:with-param name="largo" select="$largo"/>
                    </xsl:call-template>
                </xsl:with-param>
                <xsl:with-param name="col2">
                    <xsl:call-template name="CAMPOSDI">
                        <xsl:with-param name="clave" select="$cve2"/>
                        <xsl:with-param name="ejercicio" select="$gEjercicio"/>
                    </xsl:call-template>-<xsl:value-of select="$cve2"/>-<xsl:value-of select="starts-with($cve2,'HEADER')"/>
                </xsl:with-param>
                <xsl:with-param name="val2">
                    <xsl:call-template name="VALORDI">
                        <xsl:with-param name="clave" select="$cve2"/>
                        <xsl:with-param name="valor" select="$val2"/>
                        <xsl:with-param name="relleno" select="$relleno"/>
                        <xsl:with-param name="largo" select="$largo"/>
                    </xsl:call-template>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:if>
    </xsl:for-each>
</xsl:template>
<xsl:template name="RENGLON">
    <xsl:param name="col1"/>
    <xsl:param name="val1"/>
    <xsl:param name="col2"/>
    <xsl:param name="val2"/>
    <fo:table padding="0" text-align="center" width="580px" space-before.optimum="5pt" space-after.optimum="2pt">
        <fo:table-column column-width="5px"/>
        <fo:table-column column-width="40px"/>
        <fo:table-column column-width="120px"/>
        <fo:table-column column-width="110px"/>
        <fo:table-column column-width="20px"/>
        <fo:table-column column-width="40px"/>
        <fo:table-column column-width="120px"/>
        <fo:table-column column-width="110px"/>
        <fo:table-column column-width="15px"/>
        <fo:table-body>
            <fo:table-row space-before.optimum="5pt" space-after.optimum="2pt">
                <fo:table-cell number-columns-spanned="9">
                    <fo:block/>
                </fo:table-cell>
            </fo:table-row>
            <fo:table-row space-before.optimum="5pt" space-after.optimum="2pt" keep-with-previous="always">
                <fo:table-cell>
                    <fo:block/>
                </fo:table-cell>
                <fo:table-cell padding-start="0pt" padding-end="0pt" padding-before="0pt" padding-after="0pt" display-align="center" text-align="start"/>
                <fo:table-cell padding-start="0pt" padding-end="0pt" padding-before="0pt" padding-after="0pt" display-align="center" text-align="start">
                    <fo:block font-size="6.0pt">
                        <xsl:value-of select="$col1"/>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell padding-start="0pt" padding-end="0pt" padding-before="0pt" padding-after="0pt" display-align="center" text-align="start">
                    <fo:block text-align="right" font-size="6.0pt" border="thin" border-right-color="#ADB3D3" border-right-style="solid" border-left-color="#ADB3D3" border-left-style="solid" border-top-color="#ADB3D3" border-top-style="solid" border-bottom-color="#ADB3D3" border-bottom-style="solid">
                        <xsl:value-of select="$val1"/>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block/>
                </fo:table-cell>
                <fo:table-cell padding-start="0pt" padding-end="0pt" padding-before="0pt" padding-after="0pt" display-align="center" text-align="start"/>
                <fo:table-cell padding-start="0pt" padding-end="0pt" padding-before="0pt" padding-after="0pt" display-align="center" text-align="start">
                    <fo:block font-size="6.0pt">
                        <xsl:value-of select="$col2"/>
                    </fo:block>
                </fo:table-cell>
                <fo:table-cell padding-start="0pt" padding-end="0pt" padding-before="0pt" padding-after="0pt" display-align="center" text-align="start">
                    <xsl:if test="$col2 and string-length($col2) > 0">
                        <fo:block text-align="right" font-size="6.0pt" border="thin" border-right-color="#ADB3D3" border-right-style="solid" border-left-color="#ADB3D3" border-left-style="solid" border-top-color="#ADB3D3" border-top-style="solid" border-bottom-color="#ADB3D3" border-bottom-style="solid">
                            <xsl:value-of select="$val2"/>
                        </fo:block>
                    </xsl:if>
                    <fo:block/>
                </fo:table-cell>
                <fo:table-cell>
                    <fo:block/>
                </fo:table-cell>
            </fo:table-row>
        </fo:table-body>
    </fo:table>
</xsl:template>

我需要删除节点: SAT_OTROS_EST_VEC SAT_BONOS_CARB_VEC SAT_COMPENSAC_VEC SAT_ACREDIT_VEC 头1 HEADER7 HEADER8

当apears,但只是为了画桌子,因为我以后会用它。

你能帮我解决一下吗?

由于

1 个答案:

答案 0 :(得分:0)

您可以使用空模板

<xsl:template match="SAT_OTROS_EST_VEC|SAT_BONOS_CARB_VEC|SAT_COMPENSAC_VEC|SAT_ACREDIT_VEC|HEADER1|HEADER7|HEADER8/">

如果您想在文件的其他部分使用这些内容,请使用匹配模板,但这一次使用不同的mode

<xsl:template match="SAT_OTROS_EST_VEC mode="other">
    ....
</xsl:template>