开发者

remove, move and rename nodes

开发者 https://www.devze.com 2023-03-28 14:34 出处:网络
I need rename all nodes <\'record> in <\'array>. The name should be the same as the name of its array. Then move <\'r开发者_运维问答ecord> +1 up. And finally remove array.

I need rename all nodes <'record> in <'array>. The name should be the same as the name of its array. Then move <'r开发者_运维问答ecord> +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.


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:

<xsl:template match="array[@type='service']">
  <xsl:apply-templates />
</xsl:template>

This will override the more specific match="*[@name]" template match when placed after it.

0

精彩评论

暂无评论...
验证码 换一张
取 消