1

私は次のXML構造を持っています:

<Main>
    <Node1>Definite</Node1>
    <Node2>Definite</Node2>
    <Node3>Definite</Node3>
    <Node4>Definite</Node4>
    <Node5>Definite</Node5>
    <Node6>Definite</Node6>
    <A>Possible</A>
    <B>Possible</B>
    <C>Possible</C>
    <D>Possible</D>    
    <E>Possible</E>
    <F>Possible</F>
    <G>Possible</G>
    <H>Possible</H>
    <I>Possible</I>
</Main>

個々の文字という名前のノード。<A>他のすべてのノードが明確であるのに、XML構造内に存在しない可能性のあるノードです。

構造内にノードを挿入して<ZZZ>、常に以下に示す位置に配置する必要があります。

<Main>
    <Node1>Value</Node1>
    <Node2>Value</Node2>
    <Node3>Value</Node3>
    <Node4>Value</Node4>
    <Node5>Value</Node5>
    <Node6>Value</Node6>
    <A>Value</A>
    <B>Value</B>
    <C>Value</C>
    <D>Value</D>    
    <E>Value</E>
    <ZZZ>Value</ZZZ>
    <F>Value</F>
    <G>Value</G>
    <H>Value</H>
    <I>Value</I>
</Main>

つまり、ノード<E>と言って<C><H>存在しなかったとすると、次のようになります。

<Main>
    <Node1>Value</Node1>
    <Node2>Value</Node2>
    <Node3>Value</Node3>
    <Node4>Value</Node4>
    <Node5>Value</Node5>
    <Node6>Value</Node6>
    <A>Value</A>
    <B>Value</B>
    <D>Value</D>    
    <ZZZ>Value</ZZZ>
    <F>Value</F>
    <G>Value</G>
    <I>Value</I>
</Main>

これが十分に明確に説明されていることを願っています:)

4

2 に答える 2

2

それは、どの要素が必要で、どの要素がオプションであるかによって異なります。たとえば、<F> が必要であると言える場合、F 要素の前に ZZZ 要素を挿入できます。

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

<xsl:template match="F">
    <ZZZ>Value</ZZZ>
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:template>

必要な要素があると言えない場合は、Main-Element のテンプレートに挿入する必要があります。

<xsl:template match="Main">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="Node1|Node2|Node3|Node4|Node5|Node6|A|B|C|D|E"/>
        <ZZZ>Value</ZZZ>
        <xsl:apply-templates select="F|G|H|I"/>
    </xsl:copy>
</xsl:template>
于 2012-04-05T10:31:03.167 に答える
0

動的に指定された名前と、任意の数の明確な名前または可能な名前で動作するように適応できる、より一般的なソリューション:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my" exclude-result-prefixes="my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <my:occurences>
    <definites>
        <definite>Node1</definite>
        <definite>Node2</definite>
        <definite>Node3</definite>
        <definite>Node4</definite>
        <definite>Node5</definite>
        <definite>Node6</definite>
    </definites>
    <possibles>
        <possible>A</possible>
        <possible>B</possible>
        <possible>C</possible>
        <possible>D</possible>
        <possible>E</possible>
        <possible>F</possible>
        <possible>G</possible>
        <possible>H</possible>
        <possible>I</possible>
    </possibles>
 </my:occurences>

 <xsl:variable name="vOccurencies" select=
   "document('')/*/my:occurences"/>

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

 <xsl:template match="/*">
  <xsl:copy>
   <xsl:apply-templates select=
    "*[name() =
        ($vOccurencies/definites/definite
        |
         $vOccurencies/possibles/possible
                  [not(position()
                  >
                   string-length(substring-before($vOccurencies/possibles, 'F')))
                   ]
         )
       ]"/>
   <ZZZ>Value</ZZZ>
   <xsl:apply-templates select=
   "*[name()
     =
      $vOccurencies/possibles/possible
                  [position()
                  >
                   string-length(
                      substring-before($vOccurencies/possibles, 'F')
                                )
                   ]
     ]"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

この変換が提供された XML ドキュメントに適用される場合:

<Main>
    <Node1>Value</Node1>
    <Node2>Value</Node2>
    <Node3>Value</Node3>
    <Node4>Value</Node4>
    <Node5>Value</Node5>
    <Node6>Value</Node6>
    <A>Value</A>
    <B>Value</B>
    <C>Value</C>
    <D>Value</D>
    <E>Value</E>
    <ZZZ>Value</ZZZ>
    <F>Value</F>
    <G>Value</G>
    <H>Value</H>
    <I>Value</I>
</Main>

必要な正しい結果が生成されます。

<Main>
   <Node1>Value</Node1>
   <Node2>Value</Node2>
   <Node3>Value</Node3>
   <Node4>Value</Node4>
   <Node5>Value</Node5>
   <Node6>Value</Node6>
   <A>Value</A>
   <B>Value</B>
   <C>Value</C>
   <D>Value</D>
   <E>Value</E>
   <ZZZ>Value</ZZZ>
   <F>Value</F>
   <G>Value</G>
   <H>Value</H>
   <I>Value</I>
</Main>

:

実際の状況では、my:occurances要素は個別の XML ドキュメントに含まれます。したがって、XSLT コードにはハードコーディングされた要素名がなく、発生 xml ドキュメントが変更されたときに変更する必要はありません。

于 2012-04-05T13:00:08.853 に答える