2016-08-26 5 views
0

XSLTの新機能XSLT構文を書くのを手伝ってもらえますか: 入力Xmlを以下に示す出力に変換する必要があります。 Id '9'と '1'だけのノードを選択する必要があります。XSLT:いくつかのノードしかコピーしない

My input XML: 
<contacts> 
<contact> 
    <id>1234567</id> 
    <firstname>John</firstname> 
    <lastname>Smith</lastname> 
    <fields type="array"> 
     <field id="4" name="Gender">M 
     </field> 
     <field id="9" name="DOB">10/10/1961 
     </field> 
     <field id="1" name="Mobile">2132312435 
     </field> 
     <field id="7" name="E-mail">[email protected] 
     </field> 
    </fields> 
</contact> 
<contact> 
    <id>1234567</id> 
    <firstname>John</firstname> 
    <lastname>Smith</lastname> 
    <fields type="array"> 
     <field id="4" name="Gender">M 
     </field> 
     <field id="9" name="DOB">12/12/1956 
     </field> 
     <field id="1" name="Mobile">234523452345 
     </field> 
     <field id="7" name="E-mail">[email protected] 
     </field> 
    </fields> 
</contact> 
</contacts> 


The output I want: 
<contacts> 
<contact> 
    <id>1234567</id> 
    <firstname>John</firstname> 
    <lastname>Smith</lastname> 
    <fields type="array"> 
     <field id="9" name="DOB">10/10/1961 
     </field> 
     <field id="1" name="Mobile">2132312435 
     </field> 
    </fields> 
</contact> 
<contact> 
    <id>1234567</id> 
    <firstname>Pete</firstname> 
    <lastname>Kelly</lastname> 
    <fields type="array"> 
     <field id="9" name="DOB">12/12/1956 
     </field> 
     <field id="1" name="Mobile">234523452345 
     </field> 
    </fields> 
</contact> 
</contacts> 

接触を通じて基本的にループしてIDを取得するには、ファーストネーム、姓とあなたのXSLではIDを持つフィールド事前

答えて

0

単純なのは、不要なノードを削除するためのIDテンプレートとオーバーライドテンプレートだけです。より詳しく

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

    <xsl:strip-space elements="*"/> 
    <xsl:output indent="yes"/> 

    <!-- identity template --> 

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

    <!-- If field ID is not equal to 9 and 1, delete it --> 
    <xsl:template match="field[@id!=9 and @id!=1]"/> 

</xsl:stylesheet> 
+0

グレート。みんなありがとう。 – Josf

0

9と1

おかげで、あなたのフィールド内のフィールドの要素をループするとき選択範囲に選択基準を追加することができます。

<xsl:for-each select="field[@id='9' or @id='1']"> 
    <!-- do something --> 
</xsl:for-each> 

セレクト= "フィールド[ID @ = 9又は@ ID = 1]" basicly手段は、条件に合致するフィールド要素を選択[ID @ = 9又は@ ID = 1]:例えば

。 @は属性を参照し、フィールド要素内の要素を調べることはありません。

関連する問題