XSLT help

Discussion in 'XML' started by silver_sabrina@hotmail.com, Jan 28, 2007.

  1. Guest

    Hey everyone, I'm having some trouble with this. I need to convert one
    xml doc into another and I think that XSLT may be the answer, so I'd
    like some help from the gurus out here :) If XSLT cannot solve the
    problem then I think I'll have to write a Java program using DOM or
    something.

    Here is an example of what I am trying to accomplish - basically, I
    need to do a lookup on the author's first name and last name and then
    create the nested XML structure containing book info by that author

    Source XML:

    <books>
    <author>
    <fname>William</fname>
    <lname>Shakespeare</lame>
    <bookName>Merchant of Venice</bookName>
    <bookRank>One</bookRank>
    </author>
    <author>
    <fname>William</fname>
    <lname>Shakespeare</lame>
    <bookName>As You Like It</bookName>
    <bookRank>Two</bookRank>
    </author>
    </books>

    Needs to be transformed to:

    <books>
    <author>
    <fname>William</fname>
    <lname>Shakespeare</lname>
    <books>
    <book>
    <bookName>Merchant of Venice</bookName>
    <bookRank>One</bookRank>
    </book>
    <book>
    <bookName>As You Like It</bookName>
    <bookRank>Two</bookRank>
    </book>
    </books>
    </author>
    </books>

    I'm not an expert in XSL so don't know if this is even possible?
    Thanks,
    Sabrina
    , Jan 28, 2007
    #1
    1. Advertising

  2. wrote:
    > Hey everyone, I'm having some trouble with this. I need to convert one
    > xml doc into another and I think that XSLT may be the answer, so I'd
    > like some help from the gurus out here :) If XSLT cannot solve the
    > problem then I think I'll have to write a Java program using DOM or
    > something.
    >
    > Here is an example of what I am trying to accomplish - basically, I
    > need to do a lookup on the author's first name and last name and then
    > create the nested XML structure containing book info by that author
    >
    > Source XML:
    >
    > <books>
    > <author>
    > <fname>William</fname>
    > <lname>Shakespeare</lame>
    > <bookName>Merchant of Venice</bookName>
    > <bookRank>One</bookRank>
    > </author>
    > <author>
    > <fname>William</fname>
    > <lname>Shakespeare</lame>
    > <bookName>As You Like It</bookName>
    > <bookRank>Two</bookRank>
    > </author>
    > </books>
    >
    > Needs to be transformed to:
    >
    > <books>
    > <author>
    > <fname>William</fname>
    > <lname>Shakespeare</lname>
    > <books>
    > <book>
    > <bookName>Merchant of Venice</bookName>
    > <bookRank>One</bookRank>
    > </book>
    > <book>
    > <bookName>As You Like It</bookName>
    > <bookRank>Two</bookRank>
    > </book>
    > </books>
    > </author>
    > </books>
    >
    > I'm not an expert in XSL so don't know if this is even possible?
    > Thanks,
    > Sabrina
    >

    in xslt2, this does what you need, he xsl1 version would be similar just
    replace xsl:for-each-group by the muenchian grouping technique (ask google)

    <xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput indent="yes"/>
    <xsl:template match="books">
    <books>
    <xsl:for-each-group select="author"
    group-by="concat(fname,'/',lname)">
    <author>
    <xsl:copy-of select="fname,lname"/>
    <books>
    <xsl:for-each select="current-group()">
    <book>
    <xsl:copy-of select="bookName,bookRank"/>
    </book>
    </xsl:for-each>
    </books>
    </author>
    </xsl:for-each-group>
    </books>
    </xsl:template>
    </xsl:stylesheet>
    David Carlisle, Jan 28, 2007
    #2
    1. Advertising

  3. David Carlisle wrote:
    > wrote:
    > replace xsl:for-each-group by the muenchian grouping technique (ask google)


    Or, more directly, see the XSLT FAQ for examples of that approach:
    http://www.dpawson.co.uk/xsl/sect2/sect21.html

    See the Grouping page, and possibly the Sorting and Grouping page as well.

    (If you're working in XSLT, Dave Pawson's collection of tips gathered
    from the XSLT Users' mailing list is a *wonderful* resource -- it
    summarizes and organizes years of experience in making XSLT do things
    ranging from simple to "I thought that was impossible.")

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
    Joe Kesselman, Jan 28, 2007
    #3
  4. Guest

    That is just beautiful! I know about the muenchian grouping technique
    but I think its such a pain to use. Its great that XSL2.0 has this
    feature.

    FYI, I'm using SAXON as it supports xsl 2.0.

    Sample Java Code:

    public static void main(String[] args) throws Exception
    {
    System.setProperty("javax.xml.transform.TransformerFactory",
    "net.sf.saxon.TransformerFactoryImpl");

    String xslID = "transform.xsl" ;
    String sourceID = "file.xml";

    // Create a transform factory instance.
    TransformerFactory tfactory =
    TransformerFactory.newInstance();
    InputStream xslIS =
    new BufferedInputStream(new FileInputStream(xslID));
    StreamSource xslSource = new StreamSource(xslIS);


    // Create a transformer for the stylesheet.
    Transformer transformer = tfactory.newTransformer(xslSource);
    InputStream xmlIS =
    new BufferedInputStream(new FileInputStream(sourceID));
    StreamSource xmlSource = new StreamSource(xmlIS);

    // Transform the source XML to System.out.
    transformer.transform(xmlSource, new
    StreamResult(System.out));
    }
    , Jan 28, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stylus Studio
    Replies:
    0
    Views:
    631
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,655
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    610
    Laurens
    Jan 26, 2004
  4. Replies:
    2
    Views:
    704
    Henry S. Thompson
    Oct 19, 2005
  5. Replies:
    4
    Views:
    651
Loading...

Share This Page