有条件地更改同一父项中的子元素

conditionally change to the child elements within the same parent

请查找输入和输出 xml 文档。任何人都可以帮助我如何更改同一个父元素中的两个子元素值,并对这些子元素中的任何一个设置条件。

输入XML:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.altova.com">
    <publisher>
        <Name id="d123">
            <Place>Chicago</Place>
        </Name
        <Location id="d1234">
        <Addr id="d234">
            <Addr1 id="d565">Illinois st</Addr1>
            <Addr2 id="d566">block a</Addr2>
            <City id="d567">chicago</City>
            <PostalCode id="d570">123456789</PostalCode>
            <StateProvCd id="d568">IL</StateProvCd>
            <PostalCodeExt id="d583">1111</PostalCodeExt>
            <County id="d574">USA</County>
        </Addr>
        </Location>
        <catalogue id="d1" >
            <cd11 id="d2">
                <title>Empire Burlesque</title>
                <artist>Bob Dylan</artist>
                <year>1985</year>
            </cd11>
        </catalogue>
        <catalogue id="d3" >
            <cd11 id="d4">
                <title>Jurassic World</title>
                <artist>Chris Pratt</artist>
            </cd11>
        </catalogue>    
    </publisher>
</root>

输出XML:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.altova.com">
    <publisher>
        <Name id="d123">
            <Place>Chicago</Place>
        </Name
        <Location id="d1234">
        <Addr id="d234">
            <Addr1 id="d565">Illinois st</Addr1>
            <Addr2 id="d566">block a</Addr2>
            <City id="d567">chicago</City>
            <PostalCode id="d570">12345</PostalCode>
            <StateProvCd id="d568">IL</StateProvCd>
            <PostalCodeExt id="d583">6789</PostalCodeExt>
            <County id="d574">USA</County>
        </Addr>
        </Location>
        <catalogue id="d1" >
            <cd11 id="d2">
                <title>Empire Burlesque</title>
                <artist>Bob Dylan</artist>
                <year>1985</year>
            </cd11>
        </catalogue>
        <catalogue id="d3" >
            <cd11 id="d4">
                <title>Jurassic World</title>
                <artist>Chris Pratt</artist>
            </cd11>
        </catalogue>    
    </publisher>
</root>

XSLT:

条件是,如果邮政编码 >= 9 (publisher/Location/Addr/PostalCode),则前 5 位数字将采用邮政编码,后 4 位数字将应用于 PostalCodeExt。我正在使用下面的 XSLT,但它只更改了邮政编码的值,因为我对邮政编码进行了匹配。我无法同时更改 PostalCodeExt 值。

<xsl:template match="publisher/Location/Addr/PostalCode">
    <xsl:variable name="postalCode" select="."/>
    <xsl:if test="string-length($postalCode) >= 9">
    <xsl:element name="PostalCode" >
        <xsl:apply-templates select="@*" />
        <xsl:value-of select="substring($postalCode, 1, 5)" />
    </xsl:element>
    </xsl:if>
    </xsl:template>

怎么样:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:alt="http://www.altova.com"
exclude-result-prefixes="alt">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="alt:PostalCode[string-length() >= 9]/text()">
    <xsl:value-of select="substring(., 1, 5)"/>
</xsl:template>

<xsl:template match="alt:PostalCodeExt[string-length(../alt:PostalCode) >= 9]/text()">
    <xsl:value-of select="substring(../../alt:PostalCode, 6)"/>
</xsl:template>

</xsl:stylesheet>

请注意,这是假设当条件不满足时,两个值都应保持原样。