2017-05-25 4 views
0

タグが<Data>の下にない場合は、タグを<Section>の下に挿入するだけでよいという要件があります。私は正しい出力を得ることができません。xslt v2.0の上位タグの状態を確認する

INPUT

<Record> 
<Data> 
    <ID>1234DFD57</ID> 
    <Group> 
     <abc-KD>243fds</abc-KD> 
    </Group> 
    <Section> 
     <ID>33-2311</ID> 
     <Group> 
      <abc-KD>NORM</abc-KD> 
     </Group> 
     <Date>2017-03-25</Date> 
    </Section> 
    <Date>2017-03-25</Date> 
</Data> 
</Record> 

期待される出力

<Record> 
<Data> 
    <ID>1234DFD57</ID> 
    <Group> 
     <abc-KD>243fds</abc-KD> 
    </Group> 
    <Section> 
     <ID>33-2311</ID> 
     <Date>2017-03-25</Date> 
    </Section> 
    <Date>2017-03-25</Date> 
</Data> 
</Record> 

XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 
<xsl:template match="Section"> 
    <xsl:copy> 
     <xsl:copy-of select="ID"/> 
     <xsl:if test="normalize-space(string(../Group)) = ''"> 
      <xsl:copy-of select="Group"/> 
     </xsl:if> 
     <xsl:copy-of select="Date"/> 
    </xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 

ご意見たとえば、非常に高く評価されています。

よろしく、

答えて

0

あなたの現在のスタイルシートは作業を行います。より効率的な方法は次のようになります。

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
    <!-- identity transform template --> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <!-- do nothing for the Group --> 
    <xsl:template match="Section[normalize-space(parent::Data/Group) != '']/Group"/> 
</xsl:stylesheet> 

アイデンティティがテンプレートを変換再帰的にドキュメントの順序でそれらを処理しながら、そのまま、そのXMLでのコピーのすべてのノードを1です。 2番目のテンプレートは、Group要素を目的の条件に一致させ、何もしないので、出力で省略します。
Section[normalize-space(parent::Data/Group) != '']/Group

それはGroup存在または(スペース文字を除く)NULL値がないData下それらSection/Group要素と一致する:@match

Xパスは、トリックを行います。

+0

ありがとうございました!出来た。テンプレートの使い方を説明できますか?申し訳ありませんが、私はまだ基​​本的な部分を学んでおり、あなたがした解決策を理解できません。ありがとうございました。 –

+0

@AltheaVeronica追加された説明をチェックしてください。 –

関連する問題