我的需要只是重新获得“名称”的属性值.如果此属性的值为“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>
- <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>