开发者

how to parse two xml tags in the same level using a for-each method using xslt?

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜