xslt merge multiple nodes with same name

老子叫甜甜 提交于 2019-12-11 10:47:32

问题


I can see many answers to similar questions, but I can't seem to get them work for me. I have some xml files with some sibling element nodes having same tag name. I want to merge these nodes using XSLT. Any help would be deeply appreciated.

Input:

<?xml version="1.0"?>
<Screen>
  <Shapes>
    <Triangle id="tri1">
    <color>red</color>
    <size>large</size>
    </Triangle>
  </Shapes>
  <Shapes>
    <Rectangle id="rec1">
      <color>blue</color>
      <size>medium</size>
    </Rectangle>
  </Shapes>
  <Shapes>
    <Circle id="cir1">
      <color>green</color>
      <size>small</size>
    </Circle>
  </Shapes>
  <Shapes>
    <Square id="sqr1">
      <color>yellow</color>
      <size>large</size>
    </Square>
  </Shapes>
  <Device>
    <Name>peg</Name>
    <type>X11</type>
  </Device>
  <Utilities>
    <Software>QT</Software>
    <Platform>Linux</Platform>
  </Utilities>
</Screen>

I want to merge all "Shapes" nodes. Required Output

<?xml version="1.0"?>
<Screen>
  <Shapes>
    <Triangle id="tri1">
      <color>red</color>
      <size>large</size>
    </Triangle>

    <Rectangle id="rec1">
      <color>blue</color>
      <size>medium</size>
    </Rectangle>

    <Circle id="cir1">
      <color>green</color>
      <size>small</size>
    </Circle>

    <Square id="sqr1">
      <color>yellow</color>
      <size>large</size>
    </Square>
  </Shapes>
  <Device>
    <Name>peg</Name>
    <type>X11</type>
  </Device>
  <Utilities>
    <Software>QT</Software>
    <Platform>Linux</Platform>
  </Utilities>
</Screen>

XSLT I tried was:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:output indent="yes" />
  <xsl:template match="Shapes">
    <xsl:if test="not(preceding-sibling::*[local-name() = 'Shapes'])">

      <Shapes>
        <xsl:apply-templates select="node() | @*" />
        <xsl:apply-templates select="following-sibling::*[local-name() = 'Shapes']" />
      </Shapes>
    </xsl:if>
    <xsl:if test="preceding-sibling::*[local-name() = 'Shapes']">
      <xsl:apply-templates select="node() | @*" />
    </xsl:if>
  </xsl:template>
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

But the output I got was ( :( )

<Screen>
<Shapes>
    <Triangle id="tri1">
      <color>red</color>
      <size>large</size>
    </Triangle>

    <Rectangle id="rec1">
      <color>blue</color>
      <size>medium</size>
    </Rectangle>

    <Circle id="cir1">
      <color>green</color>
      <size>small</size>
    </Circle>

    <Square id="sqr1">
      <color>yellow</color>
      <size>large</size>
    </Square>
  </Shapes>

    <Rectangle id="rec1">
      <color>blue</color>
      <size>medium</size>
    </Rectangle>
    <Circle id="cir1">
      <color>green</color>
      <size>small</size>
    </Circle>
    <Square id="sqr1">
      <color>yellow</color>
      <size>large</size>
    </Square>

  <Device>
    <Name>peg</Name>
    <type>X11</type>
  </Device>
  <Utilities>
    <Software>QT</Software>
    <Platform>Linux</Platform>
  </Utilities>
</Screen>

Is there a simple XSLT code I can use, or is there any modification in my xslt I can apply to get the output?


回答1:


This should do the job:

<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="/*">
    <xsl:copy>
      <Shapes>
        <xsl:copy-of select="Shapes/*"/>
      </Shapes>
      <xsl:apply-templates select="*[name()!='Shapes']"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

The idea is to process all the sub-elements of Shapes separately in one go, and then copy the rest.



来源:https://stackoverflow.com/questions/18514292/xslt-merge-multiple-nodes-with-same-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!