2017-06-23 49 views
0

内部属性值说,这是我的XML使用XSL修改XML标签

<?xml version="1.0" encoding="UTF-8"?> 
<node> 
    <file name="abc.txt.bak"> 
     <fileid value="112358"/> 
    </file> 
    <location value="Baker Street"/> 
</node> 

我想使用XSL转换该XML,通过删除文件名.bak的。 预期的结果是这样的:

<?xml version="1.0" encoding="UTF-8"?> 
<node> 
    <file name="abc.txt"> 
     <fileid value="112358"/> 
    </file> 
    <location value="Baker Street"/> 
</node> 

我的XSL文件是这样的,这是行不通的。它只是复制一切而不改变数值。

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="node/file/@name" > 
     <xsl:copy> 
      <xsl:attribute name="name"> 
       <xsl:value-of select="substring-before(., '.bak')" /> 
      </xsl:attribute> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 
+0

的可能的复制[XSLT:如何在更改属性值(https://stackoverflow.com/questions/615875/xslt-how-to-change-an-attribute-value-during -xslcopy) – dave

+0

只需在''周围删除''包装。 –

+0

不,它不起作用 – siriuswangch

回答

0

我已经解决了我自己的问题。只是为了分享。

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="node/file"> 

     <xsl:variable name="bak_file_name"> 
      <xsl:value-of select="@name" /> 
     </xsl:variable> 

     <xsl:copy> 
      <xsl:apply-templates select="@*"/> 
       <xsl:attribute name="name"> 
          <xsl:value-of select="substring-before($bak_file_name, '.bak')" /> 
       </xsl:attribute> 
      <xsl:apply-templates select="fileid"/> 
     </xsl:copy> 


    </xsl:template> 



</xsl:stylesheet>