Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 20th, 2007, 04:49 AM
Registered User
 
Join Date: Dec 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML to XML through XSLT

I have a source and a resultant xml.Can someone provide me with an xslt for this scenario.

Source:
Code:
<text>With the Linux installation package, you receive the Linux
    Installer application that installs the software and databases. <p />Best installation
    instructions are available from HOWTO site
    (http://www.google.com/howto/). If possible, we suggest that you use
    one of these: 
        <list>
            <li><a href="http://www.google.com/howto/install_linux.html">HOWTO Install Linux</a></li>
            <li><a href="http://www.yandex.ru/howto/install_win.html">HOWTO Install Windows</a></li>
        </list>
    Linux can be extended with various plugins. If you are in need of extended functionality, please check
    <a href="http://www.google.com/">www.google.com/</a>.</text>
    </text>
Resultant:

Code:
<root>
    <text:p>
    With the Linux installation package, you receive the Linux
    Installer application that installs the software and databases. 
    </text:p>
    <text:p/>
    <text:p>
    Best installation
    instructions are available from HOWTO site
    (http://www.google.com/howto/). If possible, we suggest that you use
    one of these: 
    </text:p>
    <text:list>
        <text:list-item>
            <text:p>
                <text:a xlink:href="http://www.google.com/howto/install_linux.html">
                    <text:span>HOWTO Install Linux</text:span>
                </text:a>
            </text:p>
        </text:list-item>
        <text:list-item>
            <text:p>
                <text:a xlink:href="http://www.yandex.ru/howto/install_win.html">
                    <text:span>HOWTO Install Windows</text:span>
                </text:a>
            </text:p>
        </text:list-item>
    </text:list>
    <text:p>
    Linux can be extended with various plugins. If you are in need of extended functionality, please check
        <text:a xlink:href="http://www.google.com.ru/">
            <text:span>www.google.com</text:span>
        </text:a>
    </text:p>
</root>
 
Old December 20th, 2007, 05:14 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Generally speaking people wont just write full solutions for you because you ask nicely - that is what most people get paid to do.

Your best bet is to give it a go yourself, and if you get stuck come back and ask specific question. You might like to start with a simple web tutorial such as that provided by the W3C Schools web site. http://www.w3schools.com/xsl/default.asp

/- Sam Judson : Wrox Technical Editor -/
 
Old December 20th, 2007, 06:09 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You might be lucky and find someone who's in the Chirstmas spirit, but my own approach on this list is that I'm happy to help people who have a specific question or a specific difficulty, but I don't provide a free programming service.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 20th, 2007, 07:11 AM
Registered User
 
Join Date: Dec 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK. I understood.

I have wrote next code:

Code:
<xsl:template match="text">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()">
    <text:p>
        <xsl:value-of select="." />
    <text:p>
</xsl:template>

<xsl:template match="p">
    <text:p/>
</xsl:template>

<xsl:template match="a">
    <text:a>
        <xsl:attribute name="xlink:href">
        <xsl:value-of select="@href" />
    </xsl:attribute>
       <text:span text:style-name="T12"><xsl:apply-templates/></text:span>
    </text:a>
</xsl:template>


<xsl:template match="list">
    <text:list>
        <xsl:apply-templates/>
    </text:list>
</xsl:template>

<xsl:template match="li">
    <text:list-item>
         <text:p text:style-name="P19">
             <xsl:apply-templates/>
         </text:p>         
     </text:list-item>
</xsl:template>
But this is not exactly what I wont.
How to put <text:a> with text into the tag <text:p > without <text:list>?

 
Old December 20th, 2007, 07:48 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I had to fix a few well-formedness errors in both your XML and your XSLT code.

I think the main difficulty here is knowing when to wrap a text node in <text:p> and when not. It's difficult to work out what all the rules are from such a small sample of text. It seems to me, for example, that you want the <text:p> wrapper for text that appears inside a <text> element, but not for text that appears inside an <a> element. That suggests you need different rules for the two cases:

<xsl:template match="text/text()">
  <text:p><xsl:value-of select="."/></text:p>
</xsl:template>

<xsl:template match="a/text()">
  <xsl:value-of select="."/>
</xsl:template>

However, I'm unable to generalize from this as to what the default rule for text nodes should be, or the rule for text nodes in other contexts - there's not enough data to go on.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 20th, 2007, 08:54 AM
Registered User
 
Join Date: Dec 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have no difficult's with text wrapping. I don't understand how can I wrap some nodes with they following or preceding text in <text:p>:

Code:
<text:p>
    Linux can be extended with various plugins. If you are in need of     extended functionality, please check
        <text:a xlink:href="http://www.google.com.ru/">
            <text:span>www.google.com</text:span>
        </text:a>
    </text:p>
and some don't wrap in <text:p>:


Code:
<text:list>
        <text:list-item>
            <text:p>
                <text:a xlink:href="http://www.google.com/howto/install_linux.html">
                    <text:span>HOWTO Install Linux</text:span>
                </text:a>
            </text:p>
        </text:list-item>
    </text:list>
 
Old December 20th, 2007, 09:36 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You problem is that your original XML isn't designed very well to try to get the output you want.

Basically you have an XML structure that looks something like this:

- <text>
  - text() 'With the Linux...'
  - <p />
  - text() 'Best installation...'
  - <list>
    - <li>
      - <a>
        - text() 'HOWTO Install Linux'
    - <li>
      - <a>
        - text() 'HOWTO Install Windows'
  - text() 'Linux can be extended...'
  - <a>
    - text() 'http://www.google.com'
  - text() '.'

As you can see inside your list the text() nodes (not to be confused with your <text> elements) are nicely inside the <a> element, however at the end the <a> element is a sibling to the two text() nodes.

The outcome you are trying to get is to wrap the two text() nodes and the <a> element in one <text:p> element - which isn't going to be easy. Off the top of my head I can't think of a simple or elegant solution at all.

/- Sam Judson : Wrox Technical Editor -/
 
Old December 21st, 2007, 05:24 AM
Registered User
 
Join Date: Dec 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I know that XML isn't designed very well for my need, but I can't change this.

Can I close tag <text> when meet tag <list> and after list close that?

Thanx for your help!

 
Old December 21st, 2007, 05:43 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

XSLT doesn't write tags to an output file: it writes nodes to a tree. Later, the serializer takes the tree and turns an element node into a start-tag/end-tag pair, but in your stylesheet you have to think of instructions writing nodes - and you can't write half a node.

This means that when you have a complex problem it's a good idea sometimes to sketch the input and output trees on paper *as trees* and then analyze the correspondence between nodes in the input and nodes in the output. The difficulties tend to arise when one node in the output corresponds to a group of nodes in the input. That's known as a grouping problem, and there's a range of techniques for dealing with them.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
xml and xsl templates as input to xslt gives xml rameshnarayan XSLT 5 August 3rd, 2005 01:58 AM
XSLT read through XML to transform another XML dendenx2 XSLT 8 July 7th, 2005 08:18 PM
XSLT for complicated xml to xml transf. required doug@sirvisetti XSLT 3 June 17th, 2005 04:26 PM
merge two xml file and make new xml using xslt ketan XSLT 0 September 21st, 2004 08:48 AM
Merge XML files into a xml file using xslt lxu XML 4 November 6th, 2003 06:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.