2017-09-28 6 views
0

私はここでmy xmlのバージョンを簡略化しています。子ノードのIMP値を使用して親ノードのデータを入力します。子のIMP値のいずれかが真である場合はtrueになる必要があります。子供がいなければ、それは偽であるべきです。xslt:親ノードレベルのノードに特定の値があるかどうかをテストします。

<info> 
    <parent index='0'> 
     <name>test1</name> 
     <children> 
     </children> 
    </parent> 
    <parent index='1'> 
     <name>test2</name> 
     <children> 
      <VALUE index='0'>test3</VALUE> 
      <VALUE index='1'>test4</VALUE> 
     </children> 
    </parent> 
    <parent index='2'> 
     <name>test3</name> 
     <impvalue>true</impvalue> 
    </parent> 
    <parent index='3'> 
     <name>test4</name> 
     <impvalue>false</impvalue> 
    </parent> 
</info> 

必要な出力は、入力と出力のXMLSとして

<info> 
    <parent index='0'> 
     <name>test1</name> 
     <children> 
     </children> 
     <impvalue>false</impvalue> 
    </parent> 
    <parent index='1'> 
     <name>test2</name> 
     <children> 
      <VALUE index='0'>test3</VALUE> 
      <VALUE index='1'>test4</VALUE> 
     </children> 
     <impvalue>true</impvalue> 
    </parent> 
    <parent index='2'> 
     <name>test3</name> 
     <impvalue>true</impvalue> 
    </parent> 
    <parent index='3'> 
     <name>test4</name> 
     <impvalue>false</impvalue> 
    </parent> 
</info> 
+0

以下の通りです。 ''タグを閉じて修正し、 ''の正しいタグ名も指定してください。タグ名にスペースを入れてはいけません。 'value'が' 'の属性の場合は、その値を指定してください。 –

+0

これを修正しました。実データを提供することができないため、これは単なるサンプルです –

答えて

1

あるidentity transform

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

を使用しているとして、あなたが入力されたデータのコピーを開始することができ、類似した後、あなたは一致させることができます子ノードとして<impvalue>を持たないノードである<impvalue>を子ノードとしないので、ノードは追加される。

<xsl:template match="parent[not(impvalue)]"> 

条件を使用して、独自の子ノードを持つcount<children>のノードを確認することができます。

<xsl:when test="count(children/*) != 0"> 

完全なXSLTは、共有入力XMLが整形式XMLない

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

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

    <!-- match <parent> having no <impvalue> child --> 
    <xsl:template match="parent[not(impvalue)]"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()" /> 
      <impvalue> 
       <xsl:choose> 
        <xsl:when test="count(children/*) != 0"> 
         <xsl:value-of select="'true'" /> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:value-of select="'false'" /> 
        </xsl:otherwise> 
       </xsl:choose> 
      </impvalue> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

出力

<info> 
    <parent index="0"> 
     <name>test1</name> 
     <children /> 
     <impvalue>false</impvalue> 
    </parent> 
    <parent index="1"> 
     <name>test2</name> 
     <children> 
      <VALUE index="0">test3</VALUE> 
      <VALUE index="1">test4</VALUE> 
     </children> 
     <impvalue>true</impvalue> 
    </parent> 
    <parent index="2"> 
     <name>test3</name> 
     <impvalue>true</impvalue> 
    </parent> 
    <parent index="3"> 
     <name>test4</name> 
     <impvalue>false</impvalue> 
    </parent> 
</info> 
関連する問題