1

子が 1 つしかない場合は、xmi ノードを削除する必要があります。xmi ノードを削除すると、属性を新しいルート ノードにコピーする必要があります。

<xmi:XMI attribute="2" xmlns:xmi="http://www.omg.org/spec/XMI/20110701" xmlns:a="A">
<namespace:node attribute2="att">
...
</namespace:node>
</xmi:XMI>

私は得る必要があります

<namespace:node attribute2="att" attribute="2" xmlns:xmi="http://www.omg.org/spec/XMI/20110701" xmlns:a="A">
...
</namespace:node>

しかし、もしあれば

<xmi:XMI attribute="2" xmlns:xmi="http://www.omg.org/spec/XMI/20110701" xmlns:a="A">
<namespace:node attribute2="att">
...
</namespace:node>
<otherNode/>
</xmi:XMI>

変更を行う必要はありません。

どんな助けでも感謝します。

4

2 に答える 2

1

この XSLT 1.0 変換:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xmi="http://www.omg.org/spec/XMI/20110701">
 <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="xmi:XMI[not(*[2])]">
  <xsl:apply-templates/>
 </xsl:template>

 <xsl:template match="xmi:XMI[not(*[2])]/*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@* | ../@*"/>
     </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

次の XML ドキュメントに適用した場合:

<xmi:XMI attribute="2"
           xmlns:xmi="http://www.omg.org/spec/XMI/20110701"
           xmlns:a="A" >
        <namespace:node attribute2="att" xmlns:namespace="some:namespace">
          ...
        </namespace:node>
        <otherNode/>
</xmi:XMI>

必要な正しい結果が生成されます(変更なし):

<xmi:XMI xmlns:xmi="http://www.omg.org/spec/XMI/20110701" xmlns:a="A" attribute="2">
   <namespace:node xmlns:namespace="some:namespace" attribute2="att">
          ...
        </namespace:node>
   <otherNode/>
</xmi:XMI>

この XML ドキュメントに適用すると:

<xmi:XMI attribute="2"
           xmlns:xmi="http://www.omg.org/spec/XMI/20110701"
           xmlns:a="A" >
        <namespace:node attribute2="att" xmlns:namespace="some:namespace">
          ...
        </namespace:node>
</xmi:XMI>

ここでも、必要な正しい結果が生成されます(xmi:XMI要素が「削除」され、その属性がその唯一の子にコピーされます)。

<namespace:node xmlns:namespace="some:namespace"    
 xmlns:xmi="http://www.omg.org/spec/XMI/20110701" xmlns:a="A" 
 attribute="2" attribute2="att">
          ...
</namespace:node>
于 2012-06-01T12:10:34.590 に答える
0

試す

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0"
  xmlns:xmi="http://www.omg.org/spec/XMI/20110701">


<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="xmi:XMI[not(*[2])">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="xmi:XMI[not(*[2])]/*">
  <xsl:copy>
    <xsl:apply-templates select="../@*, @*, node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
于 2012-06-01T11:52:15.423 に答える