1

私はこのxmlを持っています:



    <?xml version="1.0" encoding="utf-8"?>
    <xml>
    <head>
        <name>This is my XML</name>
        <version>This is my XML</version>   
    </head>
    <body>
        <item id="SH2-99435">
            <properties>
                <property id="69">
                    <name>This is a property</name>
                    <amount>54.13</amount>
                    <estructure id="IZ4">
                        <name>caterpillar</name>
                        <location><zipCode>02210</zipCode><street>South Station</street></location>
                    </estructure>
                </property>
                <features id="ABC3">
                    <feature>If it works, a bug is another feature.</feature>
                    <feature>feature!!</feature>
                </features>
            </properties>
            <coding>
                <codename>Silent Hill 2</codename>
                <developer>Team Silent</developer>
                <publisher>Konami</publisher>
            </coding>
        </item>
        <item id="SH3-4498">
            <text value="it has values like the other item"/>
        </item>       
        <item id="MGS-2">
            <text value="it has values like the other item"/>
        </item>
    </body>
    </xml>


そして私はこれを達成したい:



    <?xml version="1.0" encoding="utf-8"?>
    <xml>
    <head>
        <name>This is my XML</name>
        <version>This is my XML</version>   
    </head>
    <body>
        <item>
            <properties>
                <property id="69">
                    <amount>54.13</amount>
                    <estructure id="IZ4">
                        <name>caterpillar</name>
                        <location><zipCode>02210</zipCode><street>South Station</street></location>
                    </estructure>
                </property>
            </properties>
        </item>
        <item id="SH3-4498">
            <text value="it should have properties and the selected sons"/>
        </item>       
        <item id="MGS-2" >
            <text value="it should have properties and the selected sons"/>
        </item>
    </body>   
    </xml>
    
    

私はこのようなものを持っています、それは正しくフィルタリングします:



    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

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

        <xsl:template match="xml/body/item/coding" />
        <xsl:template match="xml/body/item/properties/features" />
        <xsl:template match="xml/body/item/properties/property/name" />

    </xsl:stylesheet>


このフィルターを実行すると、それぞれ異なるタグを持つ10個のファイルが生成されると言われています。ただし、新しいタグが追加された場合は、必要なファイルにタグを追加するだけでなく、不要なタグを除外するために10個のファイルを変更する必要があります。たとえば、他のファイルでは、コーディングのみが含まれるようになります。

私はこれで試しました:

    

    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="some:ns">
        <xsl:output omit-xml-declaration="yes" indent="yes" />
        <xsl:strip-space elements="*" />
        <ns:WhiteList>
            <name>amount</name>
            <name>estructure</name>
        </ns:WhiteList>
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*" />
            </xsl:copy>
        </xsl:template>
        <xsl:template
            match="*[not(descendant-or-self::*[name()=document('')/*/ns:WhiteList/*])]" />
    </xsl:stylesheet>
    

しかし、それはestructureの子をコピーしません。私に何ができるか知っていますか?

ありがとう。

アップデート。このようにするための正当化を追加し、その逆ではなく、より説明的な質問を追加しました。

4

1 に答える 1

3

私はこの答えを受け入れませんが、あなたの当面の質問への答えとして、estructure要素の祖先 (または自己) である要素をテストしていますが、それ以外は無視します。これは、estructureの子孫が無視されることを意味します。xsl:if 条件を次のように変更します

<xsl:if 
   test="descendant-or-self::*[generate-id(.)=$copyValue2]|ancestor::*[generate-id(.)=$copyValue2]">

うん。

正直なところ、最初の XSLT ははるかに優れていてきれいです。

変換で従おうとしているルールを正確に説明していただければ、おそらく役立つでしょう。特定のノードを除くすべてをコピーするというルールがある場合、最初の XSLT はそのために理想的です。ルールが明示的なノードのみをコピーする場合、正確に何をコピーする必要があるかについてもう少し詳しく教えてください。

編集:

明確化に応じて、このXSLTを試してください

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

   <ns:WhiteList>
      <name>amount</name>
      <name>estructure</name>
   </ns:WhiteList>

   <xsl:variable name="whistList" select="document('')/*/ns:WhiteList" />

   <xsl:template match="@*">
      <xsl:copy/>
   </xsl:template>   

   <xsl:template match="*">
      <xsl:if test="descendant-or-self::*[name()=$whistList/*]|ancestor::*[name()=$whistList/*]">
         <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
         </xsl:copy>
      </xsl:if>
   </xsl:template>      
</xsl:stylesheet>

変数を使用してドキュメントのルックアップを保持することで、少し整理しようとしました。xsl:ifを使用する理由は、テンプレート マッチで変数を使用できないことに注意してください。

于 2012-04-20T13:16:18.203 に答える