请找到输入和输出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:
条件是,如果Postalcode是> = 9(发布者/位置/地址/邮政编码),则前5个数字将在Postalcode中,最后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>
答案 0 :(得分:0)
怎么样:
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>
请注意,这假设在不满足条件时,两个值都应保持不变。