-1

特定のメソッドが含まれているxmlファイル内のいくつかのノードを削除する必要があります。たとえば、次のように表示されます。

<root> 
<node id="a">
    <section id="a_1">
        <item id="0">
            <attribute>
                <color>Red</color>
            </attribute>
        </item>
    </section>

    <section id="a_2">
        <item id="0">
            <attribute>
                <color>Red</color>
            </attribute>
        </item>
    </section>            
</node>

<node id="b">
    <section id="b_1">         
        <user id="b_1b" method="pause">
            <attribute>a</attribute>
        </user>      
        <user id="b_1b" method="run">
            <attribute>a</attribute>
        </user>             
        <user id="b_1a" method="run">
            <attribute>
                <name>John</name>
            </attribute>
        </user>

        <user id="b_1a" method="pause">
            <attribute>a</attribute>
        </user>
    </section>

    <section id="b_1" method="create">   

        <user id="b_1b" method="stop">
            <attribute>a</attribute>
        </user>            
        <user id="b_1a" method="stop">
            <attribute>a</attribute>
        </user>

        <user id="b_1b" method="run">
            <attribute>a</attribute>
        </user>
        <user id="b_1b" method="pause">
            <attribute>a</attribute>
        </user>
    </section>

    <section id="b_2">                
        <user id="b_1a" method="run">
            <attribute>
                <name>John</name>
            </attribute>
        </user>
    </section>
</node>

この方法を使用する場合:

<xsl:transform version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:my="http://localhost"
  exclude-result-prefixes="my">

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

  <xsl:template match=" user[not(@method eq 'stop' )][not(. is my:last(.))]
  | user[    @method eq 'stop' ][not(. is my:last(.))]
                     | user[not(@method eq 'stop')][my:last(.)/@method eq 'stop']"/>

          <!-- Get the last user for this section & user id -->
          <xsl:function name="my:last">
            <xsl:param name="user"/>
            <xsl:sequence select="($user/../../section[@id eq $user/../@id]
                                              /user   [@id eq $user/@id]
                                  )
                                  [last()]"/>
          </xsl:function>
</xsl:transform>

結果は次のようになります。

<root>
    <node id="a">
        <section id="a_1">
            <item id="0">
                <attribute>
                    <color>
Red
                    </color>
                </attribute>
            </item>
        </section>
        <section id="a_2">
            <item id="0">
                <attribute>
                    <color>
Red
                    </color>
                </attribute>
            </item>
        </section>
    </node>
    <node id="b">
        <section id="b_1">
        </section>
        <section id="b_1" method="create">
            <user id="b_1a" method="stop">
                <attribute>
a
                </attribute>
            </user>
            <user id="b_1b" method="pause">
                <attribute>
a
                </attribute>
            </user>
        </section>
        <section id="b_2">
            <user id="b_1a" method="run">
                <attribute>
                    <name>
John
                    </name>
                </attribute>
            </user>
        </section>
    </node>
</root>

期待される出力は次のとおりです。

<root>
    <node id="a">
        <section id="a_1">
            <item id="0">
                <attribute>
                    <color>Red</color>
                </attribute>
            </item>
        </section>

        <section id="a_2">
            <item id="0">
                <attribute>
                    <color>Red</color>
                </attribute>
            </item>
        </section>            
    </node>

    <node id="b">
        <section id="b_1">                                           
        </section>

        <section id="b_1" method="create">               
            <user id="b_1a" method="stop">
                <attribute>a</attribute>
            </user>

            **<user id="b_1b" method="run">
                <attribute>a</attribute>
            </user>**
            <user id="b_1b" method="pause">
                <attribute>a</attribute>
            </user>
        </section>

        <section id="b_2">                
            <user id="b_1a" method="run">
                <attribute>
                    <name>John</name>
                </attribute>
            </user>
        </section>
    </node>
</root>

これが注文の仕組みです。'stop'が最後に発生した場合、同じユーザーID(pauseやrunなど)を持つ他のメソッドを持つ他のノードはすべて削除されます。ただし、そうでない場合は、「stop」が付いているノード自体と、その「stop」より前のすべてのノードが削除されます。

これは同じセクションIDで発生する必要があり、ユーザーノードのみが削除されます(ユーザーノードの削除後にセクションIDが空であっても、セクションIDはそのままにしておきます)。説明が混乱しないことを願っています。

どうもありがとう。

乾杯、ジョン

4

2 に答える 2

2

代わりにこのXSLTを試してください。これにより、条件の数が減り、XSLT1.0でも機能します。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

   <!-- Ignore users with 'stop' method which are not the last such user -->
   <xsl:template match="user[@method='stop'][following::user[@method='stop']]"/>

   <!-- Match other users -->
   <xsl:template match="user">
      <!-- Copy the user if there isn't a following user with the same id and 'stop' method -->
      <xsl:if test="not(following::user[@id=current()/@id][@method='stop'])">
         <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
         </xsl:copy>
      </xsl:if>
   </xsl:template>

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

これにより、期待される出力も生成されます

于 2012-04-21T11:49:38.857 に答える
0

私は答えを見つけたと思います

<xsl:transform version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:my="http://localhost"
  exclude-result-prefixes="my">

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

    <xsl:template match="user[not(@method eq 'stop')][following::user[@method eq 'stop']]
    | user[(@method eq 'stop')][not(. is my:last(.))] 
    | user[not(@method eq 'stop')][my:last(.)/@method eq 'stop']"/>

    <xsl:function name="my:last">
        <xsl:param name="user"/>
        <xsl:sequence select="($user/../../section[@id eq $user/../@id]
                                              /user   [@id eq $user/@id]
                                  )
                                  [last()]"/>
    </xsl:function>
</xsl:transform>

これがそれを解決する正しい方法であるか、誰かがより良い解決策を持っているかどうかコメントしてください。

于 2012-04-20T02:33:21.873 に答える