删除、移动和重命名节点
我需要重命名所有节点 <'record>;在<'数组>中。该名称应该与其数组的名称相同。然后移动<'记录> +1 向上。最后删除数组。
这是我的 xml:
<IDataXMLCoder version="1.0">
<record javaclass="xxx">
<value name="errorCode">0</value>
<array name="service" type="record" depth="1">
<record javaclass="xxx">
<value name="serviceName">PT</value>
<value name="serviceId">99</value>
<value name="serviceInternalId">8</value>
<value name="packageSymbol">8888</value>
<value name="serviceComment">RT</value>
<value name="serviceNameInChannel">RT</value>
<value name="serviceActivationParam">NOW</value>
<value name="serviceDeactivationParam">NOW</value>
<null name="estimatedProcessingTime"/>
<null name="virtualService"/>
</record>
<record javaclass="zzz">
<value name="serviceName">RK</value>
...
这应该看起来像:
<?xml version="1.0" encoding="UTF-8"?><IDataXMLCoder>
<record>
<errorCode>0</errorCode>
<service>
<serviceName>PT</serviceName>
<serviceId>99</serviceId>
<serviceInternalId>8</serviceInternalId>
<packageSymbol>8888</packageSymbol>
<serviceComment>RT</serviceComment>
<serviceNameInChannel>RT</serviceNameInChannel>
<serviceActivationParam>NOW</serviceActivationParam>
<serviceDeactivationParam>NOW</serviceDeactivationParam>
<estimatedProcessingTime/>
<virtualService/>
</service>
<service>
<serviceName>RK</serviceName>
...
这是我的代码:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<!--copy all-->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!--change record's name to service-->
<xsl:template match="array/record">
<xsl:element name="service">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<!--node name as attribute's name-->
<xsl:template match="*[@name]">
<xsl:element name="{@name}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
现在我看到节点 <'record>;作为<'服务>但我不知道如何向上移动并删除数组。
I need rename all nodes <'record> in <'array>. The name should be the same as the name of its array. Then move <'record> +1 up. And finally remove array.
This is my xml:
<IDataXMLCoder version="1.0">
<record javaclass="xxx">
<value name="errorCode">0</value>
<array name="service" type="record" depth="1">
<record javaclass="xxx">
<value name="serviceName">PT</value>
<value name="serviceId">99</value>
<value name="serviceInternalId">8</value>
<value name="packageSymbol">8888</value>
<value name="serviceComment">RT</value>
<value name="serviceNameInChannel">RT</value>
<value name="serviceActivationParam">NOW</value>
<value name="serviceDeactivationParam">NOW</value>
<null name="estimatedProcessingTime"/>
<null name="virtualService"/>
</record>
<record javaclass="zzz">
<value name="serviceName">RK</value>
...
This should looks like:
<?xml version="1.0" encoding="UTF-8"?><IDataXMLCoder>
<record>
<errorCode>0</errorCode>
<service>
<serviceName>PT</serviceName>
<serviceId>99</serviceId>
<serviceInternalId>8</serviceInternalId>
<packageSymbol>8888</packageSymbol>
<serviceComment>RT</serviceComment>
<serviceNameInChannel>RT</serviceNameInChannel>
<serviceActivationParam>NOW</serviceActivationParam>
<serviceDeactivationParam>NOW</serviceDeactivationParam>
<estimatedProcessingTime/>
<virtualService/>
</service>
<service>
<serviceName>RK</serviceName>
...
Here is my code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<!--copy all-->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!--change record's name to service-->
<xsl:template match="array/record">
<xsl:element name="service">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<!--node name as attribute's name-->
<xsl:template match="*[@name]">
<xsl:element name="{@name}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Now i see nodes <'record> as <'service> but i don;t know how move up and delete the array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是您的
元素正在由您的最后一个模板处理,并且正在添加额外的
元素。只需添加此模板即可将其删除:这将覆盖放置在其后面的更具体的
match="*[@name]"
模板匹配。Your problem is that your
<array name="service"..>
element is being processed by your last template, and is adding in an extra<service>
element. Just add this template to remove it:This will override the more specific
match="*[@name]"
template match when placed after it.