xml – 使用XSLT更改单个属性

前端之家收集整理的这篇文章主要介绍了xml – 使用XSLT更改单个属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
你能想到的最简单的XSLT是什么,只能将第一个的值,在这种情况下,/ configuration /system.web / compile / @ debug属性从true转换为false?
这种转变:
  1. <xsl:stylesheet version="1.0"
  2. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. >
  4. <xsl:output omit-xml-declaration="yes" indent="yes"/>
  5. <xsl:strip-space elements="*"/>
  6.  
  7. <xsl:template match="node()|@*" name="identity">
  8. <xsl:copy>
  9. <xsl:apply-templates select="node()|@*"/>
  10. </xsl:copy>
  11. </xsl:template>
  12.  
  13. <xsl:template match="system.web/compilation[1]/@debug">
  14. <xsl:attribute name="debug">false</xsl:attribute>
  15. </xsl:template>
  16. </xsl:stylesheet>

应用于此XML文档时:

  1. <configuration>
  2. <system.web>
  3. <compilation debug="true" defaultLanguage="C#">
  4. <!-- this is a comment -->
  5. </compilation>
  6.  
  7. <compilation debug="true" defaultLanguage="C#">
  8. <!-- this is another comment -->
  9. </compilation>
  10. </system.web>
  11. </configuration>

产生想要的正确结果:修改任何system.web元素的第一个编译子元素的debug属性(但我们知道配置文件中只有一个system.web元素.

  1. <configuration>
  2. <system.web>
  3. <compilation debug="false" defaultLanguage="C#">
  4. <!-- this is a comment -->
  5. </compilation>
  6. <compilation debug="true" defaultLanguage="C#">
  7. <!-- this is another comment -->
  8. </compilation>
  9. </system.web>
  10. </configuration>

正如我们所看到的,根据需要,只有第一个调试属性修改为false.

猜你在找的XML相关文章