2017-06-23 53 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>