XSLT复杂的副本

时间:2015-11-04 12:59:23

标签: xml xslt

我想用XSL表复制XML文件。通过复制,我想要“匿名”我的XML文件,将XML的一些元素作为变量。

基地:

<?xml version="1.0" encoding="ISO-8859-15"?>
<root>
    <top>
        <nbpeople>2</nbpeople>
    </top>
    <people>
        <id>45</id>
        <name>MARTIN</name>
        <firstname>Jean</firstname>
        <xyx>
            <oo>BLABLA</oo>
            <cp>11222</cp>
        </xyx>
    </people>
    <people>
        <id>98</id>
        <name>DUPONT</name>
        <firstname>Yves</firstname>
        <xyx>
            <oo>POPOPO</oo>
            <cp>33450</cp>
        </xyx>
    </people>
</root>

结果:

<?xml version="1.0" encoding="ISO-8859-15"?>
<root>
    <top>
        <nbpeople>2</nbpeople>
    </top>
    <people>
        <id>45</id>
        <name>people 45</name>
        <!-- {xyx/cp + id} = 11222 + 45 = 11267 -->
        <firstname>Jean 11267</firstname>
        <xyx>
            <oo>BLABLA</oo>
            <!-- {id + 20000} = 45 + 20000 = 20045 -->
            <cp>20045</cp>
        </xyx>
    </people>
    <people>
        <id>98</id>
        <name>people 98</name>
        <!-- {xyx/cp + id} = 33450 + 98 = 11267 -->
        <firstname>Yves 33548</firstname>
        <xyx>
            <oo>POPOPO</oo>
            <!-- {id + 20000} = 98 + 20000 = 20098 -->
            <cp>20098</cp>
        </xyx>
    </people>
</root>

我的真实XML文件显然更复杂。

编辑: 我不想要像XSL那样:

<xsl:template match="people/name">
    <xsl:variable name="id" select="../id" />
    <xsl:copy>
        <xsl:value-of select="concat('people ', $id)" />
    </xsl:copy>
</xsl:template>
<xsl:template match="people/firstname">
    <xsl:variable name="id" select="../id" />
    <xsl:copy>
        <xsl:value-of select="concat(text(), $id)" />
    </xsl:copy>
</xsl:template>

OR

<xsl:template match="people/name">
    <xsl:copy>
        <xsl:value-of select="concat('people ', ../id)" />
    </xsl:copy>
</xsl:template>
<xsl:template match="people/firstname">
    <xsl:copy>
        <xsl:value-of select="concat(text(), ../id)" />
    </xsl:copy>
</xsl:template>

我有很多处理要做,并且有多个级别的复杂文件。这将是无法管理的。

感谢的

1 个答案:

答案 0 :(得分:0)

一旦你学习了XSLT,你会发现它实际上并不复杂。首先要做的是XSLT Identity Transform

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

就其本身而言,它会按原样复制所有内容,这就是你的情况意味着你几乎就在那里!您只需要一个模板用于&#34;例外&#34; case,name元素,用于创建新值。它看起来像这样

<xsl:template match="people/name">
    <xsl:copy>
        <xsl:value-of select="concat('people ', ../id)" />
    </xsl:copy>
</xsl:template>

那就是它!试试这个XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="people/name">
        <xsl:copy>
            <xsl:value-of select="concat('people ', ../id)" />
        </xsl:copy>
    </xsl:template>

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

如果您想以类似的方式更改任何其他字段,可以添加其他模板。

编辑:如果你想让它更通用,而不是每个字段都有模板,你可以这样做:

    <xsl:template match="people/*[not(self::id)]">
        <xsl:copy>
            <xsl:value-of select="concat(local-name(), ' ', ../id)" />
        </xsl:copy>
    </xsl:template>

除了id

之外,这会对people记录下的所有字段进行匿名处理
相关问题