简体   繁体   English

XSLT:重命名属性(如果不存在)

[英]XSLT: Rename attribute if not exists

I have a lot of XML files that contain attributes with a misspelled value: 我有很多XML文件,其中包含属性的拼写错误的值:

<Part id="1">
  <Attribute Name="Colo" Value="Red" />
</Part>

Colo should be Color . Colo应该是Color Now in some files this has been corrected manually and then both attributes exist: 现在,在某些文件中,这已被手动更正,然后两个属性同时存在:

<Attribute Name="Colo" Value="Red" />
<Attribute Name="Color" Value="Blue" />

I have a XSL transformation that renames the Colo attribute to Color but I have no idea how to avoid that when the corrected attribute already exists. 我有一个XSL转换,将Colo属性重命名为Color但是我不知道当已校正的属性已经存在时如何避免这种情况。

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

<xsl:template match="Attribute/@Name[. = 'Colo']">
    <xsl:attribute name="Name">
        <xsl:value-of select="'Color'"/>
    </xsl:attribute>
</xsl:template>

How to not rename if there already is the correct attribute? 如果已经有正确的属性,如何不重命名?

I think you want to do: 我想你想做:

XSLT 1.0 XSLT 1.0

<xsl:stylesheet version="1.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="Attribute[@Name = 'Colo']">
    <xsl:if test="not(../Attribute[@Name = 'Color'])">
        <Attribute Name="Color" Value="{@Value}" />
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

This will modify the Attribute element with the misspelled name if it does not have a sibling with the correct name; 如果没有正确名称的兄弟姐妹,这将使用拼写错误的名称来修改Attribute元素。 otherwise it will just remove it. 否则,将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM