2016-11-04 6 views
0
<main> 
    <parent> 
     <parentId>parentId1</parentId> 
     <aParentField>aParentValue 
     </aParentField> 
    </parent> 
    <child> 
     <parentId>parentId1</parentId> 
     <aChildField>aChildValue 
     </aChildField> 
    </child> 
</main> 

を組み合わせると、結果は以下のようにする必要がありますので、組み合わせるためのパラメータとしてIDを使用してAとBを結合しようとしていた。私はXMLに新しいですXML、XPathのまたはXQuery

<main> 
    <parent> 
     <parentId>parentId1</parentId> 
     <aParentField>aParentValue 
     </aParentField> 
     <child> 
      <aChildField>aChildValue 
     </aChildField> 
     </child> 
    </parent> 
</main> 
何ができる

どのように使用される?

+0

あなたがする必要があります:あなたの入力が複数parentのノードを持つことができると仮定すると、複数のchildのノードにリンクされているそれぞれが、私はあなたがkeyは、相互参照を解決するために使用することをお勧めXMLFileを生成するか、結果として得られるオブジェクトは希望の形式にする必要がありますか? – Maddy

+0

結果のオブジェクトは、必要な形式にする必要があります。@ Maddy –

答えて

2

この例は少し曖昧です。

XSLT 1.0

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

<xsl:key name="child" match="child" use="parentId" /> 

<xsl:template match="/main"> 
    <xsl:copy> 
     <xsl:apply-templates select="parent"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="parent"> 
    <xsl:copy> 
     <xsl:copy-of select="*"/> 
     <xsl:apply-templates select="key('child', parentId)"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="child"> 
    <xsl:copy> 
     <xsl:copy-of select="*[not(self::parentId)]"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

もう1人の子が同じ内容のchild2と言っている場合はどうなりますか? @ michael.hor257k –

+0

@タルンそれを試してみてください。 –

+0

これは機能しましたが、入れ子要素とは別の問題があります。https://stackoverflow.com/questions/40493838/combining-xml-using-xslt-1-0 –

関連する問題