2016-10-22 109 views
0

比方说,我们有这个源XML:XSLT修改属性值

<?xml version="1.0" encoding="UTF-8"?> 
<SyncSupplierPartyMaster xmlns="http://schema.infor.com/InforOAGIS/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schema.infor.com/InforOAGIS/2 http://schema.infor.com/2.10.x/InforOAGIS/BODs/Developer/SyncSupplierPartyMaster.xsd" releaseID="9.2" versionID="2.10.x"> 
    <DataArea> 
     <SupplierPartyMaster> 
      <Classification> 
       <Codes> 
        <Code listID="CLASS" sequence="1">3</Code> 
        <Code listID="CURRENCY" sequence="2">EUR</Code> 
        <Code listID="KVK" sequence="3">-</Code> 
       </Codes> 
      </Classification> 
     </SupplierPartyMaster> 
    </DataArea> 
</SyncSupplierPartyMaster> 

我需要改变类MROClass。我有这个XSL,哪个不起作用,有人能告诉我什么是错的?转换也可以在:http://xsltransform.net/gWEamL8/1

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://schema.infor.com/InforOAGIS/2"> 
    <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> 

<!-- Rename attribute Class to MROClass --> 


<xsl:template match="@ListID[.='CLASS']"> 
    <xsl:attribute name="ListID"> 
     <xsl:value-of select="'MROClass'"/> 
    </xsl:attribute> 
</xsl:template> 

</xsl:stylesheet> 

回答

1

XSLT区分大小写。您的XML属性和listID,但您的XSLT正在寻找ListID(使用大写L)。尝试改变模板这样:

<xsl:template match="@listID[.='CLASS']"> 
    <xsl:attribute name="listID"> 
     <xsl:value-of select="'MROClass'"/> 
    </xsl:attribute> 
</xsl:template> 

需要注意的是,你并不真的需要输出的文本与xsl:value-of,你可以这样做......

<xsl:template match="@listID[.='CLASS']"> 
    <xsl:attribute name="listID"> 
     <xsl:text>MROClass</xsl:text> 
    </xsl:attribute> 
</xsl:template> 

甚至只是这样的:

<xsl:template match="@listID[.='CLASS']"> 
    <xsl:attribute name="listID">MROClass</xsl:attribute> 
</xsl:template>