xslt从递归副本替换属性值

时间:2014-09-30 08:53:56

标签: xslt

我需要用命名空间替换xml的递归副本中的属性值 属性是actionCode,它在源文件中具有值Replace。我需要用值Change替换它 这是我的档案

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SyncPulseTask releaseID="10.1.3" xmlns="http://schema.infor.com/InforOAGIS/2">
<ApplicationArea>
    <Sender>
        <LogicalID>infor.engine.pulse</LogicalID>
        <ConfirmationCode>OnError</ConfirmationCode>
    </Sender>
    <CreationDateTime>2014-09-29T15:47:27.140Z</CreationDateTime>
</ApplicationArea>
<DataArea>
    <Sync>
        <TenantID>infor</TenantID>
        <ActionCriteria>
            <ActionExpression actionCode="Replace"/>
        </ActionCriteria>
    </Sync>
</DataArea>
</SyncPulseTask>

这是我的xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns="http://schema.infor.com/InforOAGIS/2" xmlns:a="http://schema.infor.com/InforOAGIS/2"                 exclude-result-prefixes="a" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@*|node()">     <!-- source bod name -->
    <ProcessPulseTask>                     <!-- target bod name -->
      <xsl:apply-templates select="a:ApplicationArea"/>
        <xsl:apply-templates select="a:DataArea"/>
    </ProcessPulseTask>
</xsl:template>
<xsl:template match="a:ApplicationArea">
    <ApplicationArea>
        <xsl:copy-of select="*"/>
    </ApplicationArea>
</xsl:template>
<xsl:template match="a:DataArea">
    <DataArea>
        <xsl:apply-templates select="a:Sync"/>
    </DataArea>
</xsl:template>
<xsl:template match="a:Sync">             <!-- source verb type -->
  <Process>                              <!-- target verb type -->
                <xsl:copy-of select="*"/>
  </Process>
</xsl:template>
</xsl:stylesheet>

,结果是

<ProcessPulseTask xmlns="http://schema.infor.com/InforOAGIS/2">
<ApplicationArea>
    <Sender>
        <LogicalID>infor.engine.pulse</LogicalID>
        <ConfirmationCode>OnError</ConfirmationCode>
    </Sender>
    <CreationDateTime>2014-09-29T15:47:27.140Z</CreationDateTime>
</ApplicationArea>
<DataArea>
    <Process>
        <TenantID>infor</TenantID>
        <ActionCriteria>
            <ActionExpression actionCode="Replace"></ActionExpression>
        </ActionCriteria>
    </Process>
</DataArea>
</ProcessPulseTask>

如何将属性actionCode中的值Replace替换为Change?

谢谢

1 个答案:

答案 0 :(得分:1)

如果这是您想要做的唯一改变,为什么不简单地做:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="http://schema.infor.com/InforOAGIS/2"
exclude-result-prefixes="a">
<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="a:ActionExpression/@actionCode[.='Replace']">
    <xsl:attribute name="actionCode">Change</xsl:attribute>
</xsl:template>

</xsl:stylesheet>

编辑:

我没有注意到Martin Honnen指出的额外更改。您可以通过添加另一个模板来处理:

<xsl:template match="a:Sync">
    <Process xmlns="http://schema.infor.com/InforOAGIS/2">
        <xsl:apply-templates select="@*|node()"/>
    </Process>
</xsl:template>

或者,正如Martin建议的那样(并且您的版本已经存在),为整个样式表制作xmlns="http://schema.infor.com/InforOAGIS/2" 默认命名空间

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://schema.infor.com/InforOAGIS/2"
xmlns:a="http://schema.infor.com/InforOAGIS/2"
exclude-result-prefixes="a">
<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="a:ActionExpression/@actionCode[.='Replace']">
    <xsl:attribute name="actionCode">Change</xsl:attribute>
</xsl:template>

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

</xsl:stylesheet>