xml – 更改特定属性的值

前端之家收集整理的这篇文章主要介绍了xml – 更改特定属性的值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的需要只是重新获得“名称”的属性值.如果此属性的值为“default”,则应将其更改为“New”.休息一切都应该是输入xml的副本.我尝试使用下面的xsl然而它无法正常工作.
  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2. <xsl:output method="xml" indent="yes"/>
  3.  
  4. <xsl:template match="@* | node()">
  5. <xsl:copy>
  6. <xsl:apply-templates select="@* | node()"/>
  7. </xsl:copy>
  8. </xsl:template>
  9.  
  10. <xsl:template match="SORRegion[@name='default']">
  11. <xsl:attribute name="name">
  12. <xsl:value-of select="'New'"/>
  13. </xsl:attribute>
  14. <xsl:copy-of select="child::*"/>
  15. </xsl:template>
  16.  
  17. </xsl:stylesheet>

输入xml

  1. <RoutingDetails>
  2. <Service ServiceName="StatementIndicatoRSService">
  3. <SOR SORname="Globestar">
  4. <CountryCode Ctrycd="124">
  5. <SORRegion name="Test">
  6. <ConsumerName name="MYCA">
  7. <AutomationIds>
  8. <PreAutoId>
  9. <AutomationId>XA1146A</AutomationId>
  10. <AutomationId>XA1146A</AutomationId>
  11. </PreAutoId>
  12. <DefaultAutoId>
  13. <AutomationId>XA1146A</AutomationId>
  14. </DefaultAutoId>
  15. </AutomationIds>
  16. </ConsumerName>
  17. <QueueDetails>
  18. <QueueManager>MAO1</QueueManager>
  19. <ReplyQueueManager>MAO1</ReplyQueueManager>
  20. <RequestQueue>GSTAR.ICS.DP.DHIPO211.REQUEST</RequestQueue>
  21. <ReplyQueue>ICS.DP.REPLY</ReplyQueue>
  22. </QueueDetails>
  23.  
  24. </SORRegion>
  25. <SORRegion name="default">
  26. <ConsumerName name="MYCA">
  27. <AutomationIds>
  28. <PreAutoId>
  29. <AutomationId>XA1146A</AutomationId>
  30. <AutomationId>XA1146A</AutomationId>
  31. </PreAutoId>
  32. <DefaultAutoId>
  33. <AutomationId>XA1146A</AutomationId>
  34. </DefaultAutoId>
  35. </AutomationIds>
  36. </ConsumerName>
  37. <QueueDetails>
  38. <QueueManager>MAO1</QueueManager>
  39. <ReplyQueueManager>MAO1</ReplyQueueManager>
  40. <RequestQueue>DEFAULT.REQUEST</RequestQueue>
  41. <ReplyQueue>ICS.DP.REPLY</ReplyQueue>
  42. </QueueDetails>
  43.  
  44. </SORRegion>
  45. </CountryCode>
  46. </SOR>
  47. </Service>
  48. </RoutingDetails>
  1. <xsl:template match="SORRegion[@name='default']">
  2. <xsl:attribute name="name">
  3. <xsl:value-of select="'New'"/>
  4. </xsl:attribute>
  5. <xsl:copy-of select="child::*"/>
  6. </xsl:template>

代码存在许多问题.最重要的问题是它删除当前节点(SORRegion元素)并仅用属性替换它.

解决方案是匹配应更新的属性.

这种转变:

  1. <xsl:stylesheet version="1.0"
  2. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:output omit-xml-declaration="yes" indent="yes"/>
  4. <xsl:strip-space elements="*"/>
  5.  
  6. <xsl:template match="node()|@*">
  7. <xsl:copy>
  8. <xsl:apply-templates select="node()|@*"/>
  9. </xsl:copy>
  10. </xsl:template>
  11.  
  12. <xsl:template match="SORRegion/@name[.='default']">
  13. <xsl:attribute name="name">
  14. <xsl:value-of select="'New'"/>
  15. </xsl:attribute>
  16. </xsl:template>
  17. </xsl:stylesheet>

当应用于提供的XML文档时:

  1. <RoutingDetails>
  2. <Service ServiceName="StatementIndicatoRSService">
  3. <SOR SORname="Globestar">
  4. <CountryCode Ctrycd="124">
  5. <SORRegion name="Test">
  6. <ConsumerName name="MYCA">
  7. <AutomationIds>
  8. <PreAutoId>
  9. <AutomationId>XA1146A</AutomationId>
  10. <AutomationId>XA1146A</AutomationId>
  11. </PreAutoId>
  12. <DefaultAutoId>
  13. <AutomationId>XA1146A</AutomationId>
  14. </DefaultAutoId>
  15. </AutomationIds>
  16. </ConsumerName>
  17. <QueueDetails>
  18. <QueueManager>MAO1</QueueManager>
  19. <ReplyQueueManager>MAO1</ReplyQueueManager>
  20. <RequestQueue>GSTAR.ICS.DP.DHIPO211.REQUEST</RequestQueue>
  21. <ReplyQueue>ICS.DP.REPLY</ReplyQueue>
  22. </QueueDetails>
  23.  
  24. </SORRegion>
  25. <SORRegion name="default">
  26. <ConsumerName name="MYCA">
  27. <AutomationIds>
  28. <PreAutoId>
  29. <AutomationId>XA1146A</AutomationId>
  30. <AutomationId>XA1146A</AutomationId>
  31. </PreAutoId>
  32. <DefaultAutoId>
  33. <AutomationId>XA1146A</AutomationId>
  34. </DefaultAutoId>
  35. </AutomationIds>
  36. </ConsumerName>
  37. <QueueDetails>
  38. <QueueManager>MAO1</QueueManager>
  39. <ReplyQueueManager>MAO1</ReplyQueueManager>
  40. <RequestQueue>DEFAULT.REQUEST</RequestQueue>
  41. <ReplyQueue>ICS.DP.REPLY</ReplyQueue>
  42. </QueueDetails>
  43.  
  44. </SORRegion>
  45. </CountryCode>
  46. </SOR>
  47. </Service>
  48. </RoutingDetails>

产生完全想要的,正确的结果:

  1. <RoutingDetails>
  2. <Service ServiceName="StatementIndicatoRSService">
  3. <SOR SORname="Globestar">
  4. <CountryCode Ctrycd="124">
  5. <SORRegion name="Test">
  6. <ConsumerName name="MYCA">
  7. <AutomationIds>
  8. <PreAutoId>
  9. <AutomationId>XA1146A</AutomationId>
  10. <AutomationId>XA1146A</AutomationId>
  11. </PreAutoId>
  12. <DefaultAutoId>
  13. <AutomationId>XA1146A</AutomationId>
  14. </DefaultAutoId>
  15. </AutomationIds>
  16. </ConsumerName>
  17. <QueueDetails>
  18. <QueueManager>MAO1</QueueManager>
  19. <ReplyQueueManager>MAO1</ReplyQueueManager>
  20. <RequestQueue>GSTAR.ICS.DP.DHIPO211.REQUEST</RequestQueue>
  21. <ReplyQueue>ICS.DP.REPLY</ReplyQueue>
  22. </QueueDetails>
  23. </SORRegion>
  24. <SORRegion name="New">
  25. <ConsumerName name="MYCA">
  26. <AutomationIds>
  27. <PreAutoId>
  28. <AutomationId>XA1146A</AutomationId>
  29. <AutomationId>XA1146A</AutomationId>
  30. </PreAutoId>
  31. <DefaultAutoId>
  32. <AutomationId>XA1146A</AutomationId>
  33. </DefaultAutoId>
  34. </AutomationIds>
  35. </ConsumerName>
  36. <QueueDetails>
  37. <QueueManager>MAO1</QueueManager>
  38. <ReplyQueueManager>MAO1</ReplyQueueManager>
  39. <RequestQueue>DEFAULT.REQUEST</RequestQueue>
  40. <ReplyQueue>ICS.DP.REPLY</ReplyQueue>
  41. </QueueDetails>
  42. </SORRegion>
  43. </CountryCode>
  44. </SOR>
  45. </Service>
  46. </RoutingDetails>

猜你在找的XML相关文章