0

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

1
  • yes...i am using for-each for "content/book"..but in the output, i need author tag as a subtag of book which is not a case in input...whatever i tried is giving me always first book's author...
    – Vijay C
    Mar 3, 2010 at 10:58

1 Answer 1

2

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.