xslt - Merging XML in different sequences -


i know there have been other questions on topic, i'm looking not there.

what want combine 2 xml files, not 1 xml file after another.

for example first xml file game.xml:

<forsale> <game>     <title>mass effect 3</title>     <releasedate>         <yyyy>2012</yyyy>         <mm>03</mm>         <dd>06</dd>     </releasedate>     <esrbrating>m</esrbrating>     <platforms>         <platform>x360</platform>     </platforms> </game> <game>     <title>borderlands 2</title>     <releasedate>         <yyyy>2012</yyyy>         <mm>09</mm>         <dd>18</dd>     </releasedate>     <esrbrating>m</esrbrating>     <platforms>         <platform>pc</platform>     </platforms> </game> 

and second xml file reviews.xml

<reviews> <game>     <title>mass effect 3</title>     <review>         <critic>kevin vanord</critic>         <pros>             <pro><![cdata[fantastic, moving story balances plot , character]]></pro>         </pros>         <cons>             <con><![cdata[some glitches , bugs]]></con>         </cons>     </review> </game>  <game>     <title>borderlands 2</title>     <review>         <critic>chris watters</critic>         <pros>             <pro><![cdata[a ton of great mission writing , dialogue]]></pro>         </pros>         <cons>             <con><![cdata[spoken messages interrupt each other]]></con>         </cons>     </review> </game> 

and want resulting master xml this:

<forsale> <game>     <title>mass effect 3</title>     <releasedate>         <yyyy>2012</yyyy>         <mm>03</mm>         <dd>06</dd>     </releasedate>     <esrbrating>m</esrbrating>     <platforms>         <platform>x360</platform>     </platforms>     <review>         <critic>kevin vanord</critic>         <synopsis>             mass effect 3 remarkably satisfying conclusion beloved trilogy, , poignant , memorable role-playing action game in own right.         </synopsis>         <pros>             <pro>fantastic, moving story balances plot , character</pro>         </pros>         <cons>             <con>some glitches , bugs</con>         </cons>     </review> </game> <game>     <title>borderlands 2</title>     <releasedate>         <yyyy>2012</yyyy>         <mm>09</mm>         <dd>18</dd>     </releasedate>     <esrbrating>m</esrbrating>     <platforms>         <platform>pc</platform>     </platforms>     <review>         <critic>chris watters</critic>         <synopsis>             stellar writing , host of small improvements borderlands 2 stand tall on shoulders of predecessor.         </synopsis>         <pros>             <pro>a ton of great mission writing , dialogue</pro>         </pros>         <cons>             <con>spoken messages interrupt each other</con>         </cons>     </review> </game> 

i tried code, outputs games first reviews

<xsl:template match="/forsale"> <xsl:copy>     <xsl:apply-templates select="game"/>     <xsl:apply-templates select="document('reviews.xml')/reviews/game/review"/>     </xsl:copy> </xsl:template> <xsl:template match =" @* | node()"> <xsl:copy>   <xsl:apply-templates select="@* | node() | text()"/> </xsl:copy> </xsl:template> 

this because line...

<xsl:apply-templates select="document('reviews.xml')/reviews/game/review"/> 

is independent previous line select games, , copy games, not specific line.

what need more line template matches game element, , amend output selected game only

<xsl:apply-templates select="document('reviews.xml')/reviews/game[title=current()/title]/review"/> 

try xslt

<xsl:template match="/forsale">    <xsl:copy>        <xsl:apply-templates select="game"/>     </xsl:copy> </xsl:template>  <xsl:template match="game">    <xsl:copy>        <xsl:apply-templates select="@* | node() | text()"/>        <xsl:apply-templates select="document('reviews.xml')/reviews/game[title=current()/title]/review"/>    </xsl:copy> </xsl:template>  <xsl:template match =" @* | node()">    <xsl:copy>      <xsl:apply-templates select="@* | node() | text()"/>    </xsl:copy> </xsl:template> 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -