tags:

views:

49

answers:

1

I am converting a xml using xslt.

Original XML is

<Content>
  <book>
     <customData>
          <CustomDataElement>
             <title>book-name</title>
             <value>Java</value>
          </CustomDataElement>
          <CustomDataElement>
             <title>genre</title>
             <value>Programming</value>
          </CustomDataElement>
     </customData>
  </book>
  <authors>
    <author>
       <name>authorOne</name>
       <country>US</country>
    </author>
  </authors>
  <book>
     <customData>
          <CustomDataElement>
             <title>book-name</title>
             <value>Stranger</value>
          </CustomDataElement>
          <CustomDataElement>
             <title>genre</title>
             <value>Fiction</value>
          </CustomDataElement>
     </customData>
  </book>
  <authors>
    <author>
       <name>authorthree</name>
       <country>UK</country>
    </author>
  </authors>
</Content>

and my xslt is as follows

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<xsl:for-each select="Content/book">
<media>
    <book>
<xsl:apply-templates select="customData/CustomDataElement[title = 'book-name']" />
    </book>

    <genre>
<xsl:apply-templates select="customData/CustomDataElement[title = 'genre']" />
    </genre>
    <author>
    <xsl:value-of select="../authors/author/name" />
    </author>
</media>
</xsl:for-each>
</xsl:template>

<xsl:template match="CustomDataElement">
<xsl:value-of select="value" />
</xsl:template>
</xsl:stylesheet>

This gives me output as

<?xml version="1.0"?>
<media>
   <book>Java</book>
   <genre>Programming</genre>
   <author>authorOne</author>
</media>
<media>
   <book>Stranger</book>
   <genre>Fiction</genre>
   <author>authorOne</author>
</media>

I want the authors name from the tag 'authors\author' which follows the book tag.

what i am missing here ? pls help

+2  A: 

Instead of

<xsl:value-of select="../authors/author/name" />

try

<xsl:value-of select="following-sibling::authors[1]/author/name" />

Since you are in the context of a book node, this xpath says to look for the first ([1]) following sibling authors node, and to select the author/name from that.

AakashM
perfect...thanks....just one more question .....where do i find this documentation of 'following-sibling' and other like same....
Vijay C
@Vijay it's in MSDN, I often refer to http://msdn.microsoft.com/en-us/library/ms256086.aspx and you can see the documentation tree around that article.
AakashM