我的需要就是重新获得“name”的属性值。如果此属性的值为“default”,则应将其更改为“New”。休息一切都应该是输入xml的副本。我尝试使用下面的xsl然而它无法正常工作。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="SORRegion[@name='default']">
<xsl:attribute name="name">
<xsl:value-of select="'New'"/>
</xsl:attribute>
<xsl:copy-of select="child::*"/>
</xsl:template>
</xsl:stylesheet>
输入xml
<RoutingDetails>
<Service ServiceName="StatementIndicatorsService">
<SOR SORname="Globestar">
<CountryCode Ctrycd="124">
<SORRegion name="Test">
<ConsumerName name="MYCA">
<AutomationIds>
<PreAutoId>
<AutomationId>XA1146A</AutomationId>
<AutomationId>XA1146A</AutomationId>
</PreAutoId>
<DefaultAutoId>
<AutomationId>XA1146A</AutomationId>
</DefaultAutoId>
</AutomationIds>
</ConsumerName>
<QueueDetails>
<QueueManager>MAO1</QueueManager>
<ReplyQueueManager>MAO1</ReplyQueueManager>
<RequestQueue>GSTAR.ICS.DP.DHIPO211.REQUEST</RequestQueue>
<ReplyQueue>ICS.DP.REPLY</ReplyQueue>
</QueueDetails>
</SORRegion>
<SORRegion name="default">
<ConsumerName name="MYCA">
<AutomationIds>
<PreAutoId>
<AutomationId>XA1146A</AutomationId>
<AutomationId>XA1146A</AutomationId>
</PreAutoId>
<DefaultAutoId>
<AutomationId>XA1146A</AutomationId>
</DefaultAutoId>
</AutomationIds>
</ConsumerName>
<QueueDetails>
<QueueManager>MAO1</QueueManager>
<ReplyQueueManager>MAO1</ReplyQueueManager>
<RequestQueue>DEFAULT.REQUEST</RequestQueue>
<ReplyQueue>ICS.DP.REPLY</ReplyQueue>
</QueueDetails>
</SORRegion>
</CountryCode>
</SOR>
</Service>
</RoutingDetails>
答案 0 :(得分:10)
<xsl:template match="SORRegion[@name='default']">
<xsl:attribute name="name">
<xsl:value-of select="'New'"/>
</xsl:attribute>
<xsl:copy-of select="child::*"/>
</xsl:template>
此代码存在许多问题。最重要的问题是它删除当前节点(SORRegion
元素)并用一个属性替换它。
解决方案是匹配应更新的属性。
此转化:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="SORRegion/@name[.='default']">
<xsl:attribute name="name">
<xsl:value-of select="'New'"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
应用于提供的XML文档:
<RoutingDetails>
<Service ServiceName="StatementIndicatorsService">
<SOR SORname="Globestar">
<CountryCode Ctrycd="124">
<SORRegion name="Test">
<ConsumerName name="MYCA">
<AutomationIds>
<PreAutoId>
<AutomationId>XA1146A</AutomationId>
<AutomationId>XA1146A</AutomationId>
</PreAutoId>
<DefaultAutoId>
<AutomationId>XA1146A</AutomationId>
</DefaultAutoId>
</AutomationIds>
</ConsumerName>
<QueueDetails>
<QueueManager>MAO1</QueueManager>
<ReplyQueueManager>MAO1</ReplyQueueManager>
<RequestQueue>GSTAR.ICS.DP.DHIPO211.REQUEST</RequestQueue>
<ReplyQueue>ICS.DP.REPLY</ReplyQueue>
</QueueDetails>
</SORRegion>
<SORRegion name="default">
<ConsumerName name="MYCA">
<AutomationIds>
<PreAutoId>
<AutomationId>XA1146A</AutomationId>
<AutomationId>XA1146A</AutomationId>
</PreAutoId>
<DefaultAutoId>
<AutomationId>XA1146A</AutomationId>
</DefaultAutoId>
</AutomationIds>
</ConsumerName>
<QueueDetails>
<QueueManager>MAO1</QueueManager>
<ReplyQueueManager>MAO1</ReplyQueueManager>
<RequestQueue>DEFAULT.REQUEST</RequestQueue>
<ReplyQueue>ICS.DP.REPLY</ReplyQueue>
</QueueDetails>
</SORRegion>
</CountryCode>
</SOR>
</Service>
</RoutingDetails>
产生完全正确的结果:
<RoutingDetails>
<Service ServiceName="StatementIndicatorsService">
<SOR SORname="Globestar">
<CountryCode Ctrycd="124">
<SORRegion name="Test">
<ConsumerName name="MYCA">
<AutomationIds>
<PreAutoId>
<AutomationId>XA1146A</AutomationId>
<AutomationId>XA1146A</AutomationId>
</PreAutoId>
<DefaultAutoId>
<AutomationId>XA1146A</AutomationId>
</DefaultAutoId>
</AutomationIds>
</ConsumerName>
<QueueDetails>
<QueueManager>MAO1</QueueManager>
<ReplyQueueManager>MAO1</ReplyQueueManager>
<RequestQueue>GSTAR.ICS.DP.DHIPO211.REQUEST</RequestQueue>
<ReplyQueue>ICS.DP.REPLY</ReplyQueue>
</QueueDetails>
</SORRegion>
<SORRegion name="New">
<ConsumerName name="MYCA">
<AutomationIds>
<PreAutoId>
<AutomationId>XA1146A</AutomationId>
<AutomationId>XA1146A</AutomationId>
</PreAutoId>
<DefaultAutoId>
<AutomationId>XA1146A</AutomationId>
</DefaultAutoId>
</AutomationIds>
</ConsumerName>
<QueueDetails>
<QueueManager>MAO1</QueueManager>
<ReplyQueueManager>MAO1</ReplyQueueManager>
<RequestQueue>DEFAULT.REQUEST</RequestQueue>
<ReplyQueue>ICS.DP.REPLY</ReplyQueue>
</QueueDetails>
</SORRegion>
</CountryCode>
</SOR>
</Service>
</RoutingDetails>