大家好我有以下XML文件:
<?xml version="1.0" encoding="utf-8" ?>
<products siteID="629">
<product id="14321904">
<name>Taior, negru</name>
<description3>
<ul>
<li>articol de damă</li>
<li>decolteu în V</li>
<li>inserţii din piele eco</li>
<li>închidere cu un singur nasture</li>
<li>material: decolteu în V</li>
<li>inserţii din piele eco</li>
<li>închidere cu un singur nasture</li>
</ul>
</description3>
<material>100% poliester</material>
<ModelSizingDescr/>
<Measurements/>
<SupplierColor/>
</product>
<product id="14321905">
<name>Taior, negru</name>
<description3>
<ul>
<li>articol de damă</li>
<li>decolteu în V</li>
<li>inserţii din piele eco</li>
<li>închidere cu un singur nasture</li>
<li>material: decolteu în V</li>
<li>inserţii din piele eco</li>
<li>închidere cu un singur nasture</li>
</ul>
</description3>
<material>100% poliester</material>
<ModelSizingDescr/>
<Measurements/>
<SupplierColor/>
</product>
&#13;
我对XSLT有点生疏,并且没有设法做很多事情。我想要的是摆脱<li>
标签中的重复文本(我在某种程度上设法做到了)。其次,我想将此行<li>material: decolteu în V</li>
替换为节点中的内容,因此它看起来像<li>material: 100% poliester</li>
。要将处理完毕的XML文件包装起来,应该如下所示:
<products siteID="629">
<product id="14321904">
<name>Taior, negru</name>
<description3>
<ul>
<li>articol de damă</li>
<li>decolteu în V</li>
<li>inserţii din piele eco</li>
<li>închidere cu un singur nasture</li>
<li>material: 100% poliester</li>
</ul>
</description3>
<material>100% poliester</material>
<ModelSizingDescr/>
<Measurements/>
<SupplierColor/>
</product>
&#13;
欢迎任何想法和建议,谢谢!
答案 0 :(得分:0)
假设您在输出中需要两个产品。和xsl v1.0
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="li">
<xsl:if test="not(node()) or not(preceding-sibling::node()[.=string(current())])">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="li/text()[.='material: decolteu în V']">material: 100% poliester</xsl:template>
</xsl:stylesheet>
当应用于输入时,响应将是
<?xml version="1.0" encoding="UTF-8"?>
<products siteID="629">
<product id="14321904">
<name>Taior, negru</name>
<description3>
<ul>
<li>articol de damă</li>
<li>decolteu în V</li>
<li>inserţii din piele eco</li>
<li>închidere cu un singur nasture</li>
<li>material: 100% poliester</li>
</ul>
</description3>
<material>100% poliester</material>
<ModelSizingDescr/>
<Measurements/>
<SupplierColor/>
</product>
<product id="14321905">
<name>Taior, negru</name>
<description3>
<ul>
<li>articol de damă</li>
<li>decolteu în V</li>
<li>inserţii din piele eco</li>
<li>închidere cu un singur nasture</li>
<li>material: 100% poliester</li>
</ul>
</description3>
<material>100% poliester</material>
<ModelSizingDescr/>
<Measurements/>
<SupplierColor/>
</product>
</products>
答案 1 :(得分:0)
如果您的处理器支持XSLT 2.0,您可以执行以下操作:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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="ul">
<xsl:for-each select="distinct-values(li)[not(starts-with(., 'material:'))]">
<li><xsl:value-of select="."/></li>
</xsl:for-each>
<li>
<xsl:value-of select="concat('material:', ../../material)"/>
</li>
</xsl:template>
</xsl:stylesheet>
应用于您的示例(在纠正其格式良好之后!),结果:
<?xml version="1.0" encoding="UTF-8"?>
<products siteID="629">
<product id="14321904">
<name>Taior, negru</name>
<description3>
<li>articol de damă</li>
<li>decolteu în V</li>
<li>inserţii din piele eco</li>
<li>închidere cu un singur nasture</li>
<li>material:100% poliester</li>
</description3>
<material>100% poliester</material>
<ModelSizingDescr/>
<Measurements/>
<SupplierColor/>
</product>
<product id="14321905">
<name>Taior, negru</name>
<description3>
<li>articol de damă</li>
<li>decolteu în V</li>
<li>inserţii din piele eco</li>
<li>închidere cu un singur nasture</li>
<li>material:100% poliester</li>
</description3>
<material>100% poliester</material>
<ModelSizingDescr/>
<Measurements/>
<SupplierColor/>
</product>
</products>