复制具有更改的元素值XSLT的节点

时间:2018-07-23 10:33:41

标签: xslt copy

我输入了xml

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1981</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <country>USA</country>
    <company>RCA</company>
    <price>9.90</price>
    <year>1982</year>
  </cd>
</catalog>
怎么做? 对于每个“目录/ cd” 1)如果“ year” ='1985',则复制所有“ catalog / cd”元素,而不更改其值 2)else复制所有元素“ catalog / cd”而不更改值,排除元素“ company”(需要设置值“ MyCompany”)和“ country”(需要设置值“ MyCountry”)

结果XML

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>MyCountry</country>
    <company>MyCompany</company>
    <price>10.90</price>
    <year>1981</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <country>MyCountry</country>
    <company>MyCompany</company>
    <price>9.90</price>
    <year>1982</year>
  </cd>
</catalog>

2 个答案:

答案 0 :(得分:0)

有多种选择可以满足您的需求。您需要从identity template开始,它将复制输入XML原样输出。

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

接下来,您可以声明单独的模板,用于在country处修改companyyear != 1985

<xsl:template match="cd[year != '1985']/country">
    <country>MyCountry</country>
</xsl:template>
<xsl:template match="cd[year != '1985']/company">
    <company>MyCompany</company>
</xsl:template>

另一种选择是在其子元素cd的单个模板year != 1985中更改两个元素的值。

<xsl:template match="cd[year != '1985']">
    <xsl:copy>
        <xsl:apply-templates select="title | artist" />
        <country>MyCountry</country>
        <company>MyCompany</company>
        <xsl:apply-templates select="price | year" />
    </xsl:copy>
</xsl:template>

希望这会有所帮助。但是,您需要在线上和做一些有关XSLT的教程。

答案 1 :(得分:0)

 <xsl:template match="catalog">
        <catalog>
        <xsl:for-each select="cd">
            <cd>
            <title>
                <xsl:value-of select="title"/>
            </title>
            <artist>
                <xsl:value-of select="artist"/>
            </artist>
            <xsl:choose>
                <xsl:when test="not(year = 1985)">
                    <country>MY Country</country>
                    <company>My Company</company>
                </xsl:when>
                <xsl:otherwise>
                    <country><xsl:value-of select="country"/></country>
                    <company><xsl:value-of select="company"/></company>
                </xsl:otherwise>
            </xsl:choose>
            <price><xsl:value-of select="price"/></price>
            <year><xsl:value-of select="year"/></year>
            </cd>
        </xsl:for-each>
        </catalog>
    </xsl:template>
Check it, I may be helful for you