|
|
 |
| 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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |
|

November 4th, 2009, 10:52 AM
|
|
Registered User
|
|
Join Date: Nov 2009
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Convert XML file using XSLT
I need to convert the following XML
<Para>
<Unique value="998197" type="enum" />
<PgfTag value="Body" type="str" />
<Pgf>
<PgfFont>
<FFamily value="Times New Roman" type="str" />
</PgfFont>
</Pgf>
<ParaLine>
<TextRectID value="19" type="enum" />
<String>Sushil Font Time Roman </String>
<Font>
<FFamily value="Garamond" type="str" />
</Font>
<String>Sharma Font Garamond</String>
</ParaLine>
</Para>
to XML
<automatic-styles>
<style name="P1" family="paragraph">
<text-properties font-style="Regular" font-size="10.0" font-name="Times New Roman" />
</style>
<style name="T1" family="text">
<text-properties font-name="Garamond" />
</style>
</automatic-styles>
<texts>
<text style-name="P1">
Sushil Font Time Roman
<span style-name="T1">
Sharma Font Garamond
</span>
</text>
</texts>
There can be multiple Para in the input XML. so there will be multiple style as P1 , P2, P3 , T1, T2 etc
Thank you for your reply 
|

November 4th, 2009, 11:19 AM
|
 |
Wrox Author
Points: 12,735, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
|
|
Perhaps start with something along these lines:
Code:
<xsl:template match="/">
<automatic-styles>
<xsl:apply-templates select="//FFamily"/>
</
</
<xsl:template match="PGFFont/FFamily">
<style family="paragraph">
<xsl:attribute name="name">P<xsl:number level="any"/></xsl:attribute>
<text-properties font-style="Regular" font-size="10.0"
font-name="{@value}" />
</style>
</xsl:template>
<xsl:template match="Font/FFamily">
<style family="paragraph">
<xsl:attribute name="name">T<xsl:number level="any"/></xsl:attribute>
<text-properties font-style="Regular" font-size="10.0"
font-name="{@value}" />
</style>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|

November 5th, 2009, 12:10 AM
|
|
Registered User
|
|
Join Date: Nov 2009
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Thank you very much for your reply.
But still i didn't get the required output file.
Code:
Input XML is :
<Flow>
<Para>
<Pgf>
<PgfFont>
<FFamily value="Times New Roman" type="str"/>
<FWeight value="Regular" type="str"/>
<FSize value="14.0 pt" type="num"/>
</PgfFont>
</Pgf>
<ParaLine>
<String>Font Time Roman </String>
<Font>
<FFamily value="Garamond" type="str"/>
<FSize value="14.0 pt" type="num"/>
</Font>
<String>Font Garamond</String>
</ParaLine>
</Para>
<Para>
<Pgf>
<PgfFont>
<FFamily value="Times New Roman" type="str"/>
<FVar value="Regular" type="str"/>
<FSize value="10.0 pt" type="num"/>
<FColor value="Black" type="str"/>
</PgfFont>
</Pgf>
<ParaLine>
<String>ABCDEFGHG</String>
</ParaLine>
</Para>
</Flow>
Output XML is :
<automatic-styles>
<style name="P1" family="paragraph">
<text-properties font-weight="Regular" font-size="14.0 pt" font-name="Times New Roman"/>
</style>
<style name="T1" family="text">
<text-properties font-name="Garamond"/>
</style>
<style name="P2" family="paragraph">
<text-properties font-weight="Regular" font-size="10.0 pt" font-name="Times New Roman"/>
</style>
</automatic-styles>
<body>
<texts>
<text style-name="P1">Font Time Roman
<textspan style-name="T1">Font Garamond</textspan>
</text>
<text style-name="P2">ABCDEFGHG</text>
</texts>
</body>
Quote:
Originally Posted by mhkay
Perhaps start with something along these lines:
Code:
<xsl:template match="/">
<automatic-styles>
<xsl:apply-templates select="//FFamily"/>
</
</
<xsl:template match="PGFFont/FFamily">
<style family="paragraph">
<xsl:attribute name="name">P<xsl:number level="any"/></xsl:attribute>
<text-properties font-style="Regular" font-size="10.0"
font-name="{@value}" />
</style>
</xsl:template>
<xsl:template match="Font/FFamily">
<style family="paragraph">
<xsl:attribute name="name">T<xsl:number level="any"/></xsl:attribute>
<text-properties font-style="Regular" font-size="10.0"
font-name="{@value}" />
</style>
</xsl:template>
|
|

November 5th, 2009, 05:13 AM
|
 |
Wrox Author
Points: 12,735, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
|
|
I don't write complete stylesheets for people unless they are paying customers. You didn't indicate what part of the problem you were having difficulty with, so I assumed it was getting started. But it's up to you to take it further. When you hit the next obstacle, show us the code you have written, and explain which part of the problem you are stuck with.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|

November 5th, 2009, 09:46 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 Posts
|
|
Here is a sample stylesheet:
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<automatic-styles>
<xsl:apply-templates select="descendant::PgfFont | descendant::Font" mode="styles"/>
</automatic-styles>
<body>
<texts>
<xsl:apply-templates/>
</texts>
</body>
</xsl:template>
<xsl:template match="PgfFont" mode="styles">
<xsl:variable name="n"><xsl:number level="any"/></xsl:variable>
<style name="P{$n}" family="paragraph">
<text-properties>
<xsl:apply-templates mode="styles"/>
</text-properties>
</style>
</xsl:template>
<xsl:template match="Font" mode="styles">
<xsl:variable name="n"><xsl:number level="any"/></xsl:variable>
<style name="T{$n}" family="text">
<text-properties>
<xsl:apply-templates mode="styles"/>
</text-properties>
</style>
</xsl:template>
<xsl:template match="PgfFont/FFamily | Font/FFamily" mode="styles">
<xsl:attribute name="font-name">
<xsl:value-of select="@value"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="PgfFont/FWeight | Font/FWeight" mode="styles">
<xsl:attribute name="font-style">
<xsl:value-of select="@value"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="PgfFont/FSize | Font/FSize" mode="styles">
<xsl:attribute name="font-size">
<xsl:value-of select="@value"/>
</xsl:attribute>
</xsl:template>
<xsl:template match="Para">
<xsl:variable name="n">
<xsl:apply-templates select="Pgf/PgfFont"/>
</xsl:variable>
<text style-name="P{$n}">
<xsl:apply-templates select="ParaLine/String"/>
</text>
</xsl:template>
<xsl:template match="Pgf/PgfFont">
<xsl:number level="any"/>
</xsl:template>
<xsl:template match="ParaLine/String[preceding-sibling::*[1][self::Font]]">
<xsl:variable name="n">
<xsl:apply-templates select="preceding-sibling::*[1][self::Font]"/>
</xsl:variable>
<textspan style-name="T{$n}">
<xsl:apply-templates/>
</textspan>
</xsl:template>
<xsl:template match="ParaLine/Font">
<xsl:number level="any"/>
</xsl:template>
</xsl:stylesheet>
When run against the input
Code:
<Flow>
<Para>
<Pgf>
<PgfFont>
<FFamily value="Times New Roman" type="str"/>
<FWeight value="Regular" type="str"/>
<FSize value="14.0 pt" type="num"/>
</PgfFont>
</Pgf>
<ParaLine>
<String>Font Time Roman </String>
<Font>
<FFamily value="Garamond" type="str"/>
<FSize value="14.0 pt" type="num"/>
</Font>
<String>Font Garamond</String>
</ParaLine>
</Para>
<Para>
<Pgf>
<PgfFont>
<FFamily value="Times New Roman" type="str"/>
<FVar value="Regular" type="str"/>
<FSize value="10.0 pt" type="num"/>
<FColor value="Black" type="str"/>
</PgfFont>
</Pgf>
<ParaLine>
<String>ABCDEFGHG</String>
</ParaLine>
</Para>
</Flow>
it outputs
Code:
<?xml version="1.0" encoding="utf-8"?>
<automatic-styles>
<style name="P1" family="paragraph">
<text-properties font-name="Times New Roman" font-style="Regular" font-size="14.0 pt"/>
</style>
<style name="T1" family="text">
<text-properties font-name="Garamond" font-size="14.0 pt"/>
</style>
<style name="P2" family="paragraph">
<text-properties font-name="Times New Roman" font-size="10.0 pt"/>
</style>
</automatic-styles>
<body>
<texts>
<text style-name="P1">Font Time Roman <textspan style-name="T1">Font Garamond</textspan>
</text>
<text style-name="P2">ABCDEFGHG</text>
</texts>
</body>
__________________
Martin Honnen
Microsoft MVP - XML
My blog
|
|
The Following User Says Thank You to Martin Honnen For This Useful Post:
|
|

November 5th, 2009, 11:57 PM
|
|
Registered User
|
|
Join Date: Nov 2009
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Thank you very much for your post. Its really helped to solved my problems.
|

November 18th, 2009, 03:05 PM
|
|
Registered User
|
|
Join Date: Nov 2009
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Convert XML file using XSLT
can you please help to the foolowing XML
Code:
<para>
<tag name="bullet"/>
<paraline>
<string>One</string>
</paraline>
</para>
<para>
<tag name="bullet"/>
<paraline>
<string>Two</string>
</paraline>
</para>
<para>
<tag name="Number"/>
<paraline>
<string>11111</string>
</paraline>
</para>
<para>
<tag name="Number"/>
<paraline>
<string>22222</string>
</paraline>
</para>
the output should be
Code:
<mainlist name="bullet">
<list>
<text>One</text>
<list>
<list>
<text>Two</text>
<list>
</mainlist>
<mainlist name="Number">
<list>
<text>11111</text>
<list>
<list>
<text>22222</text>
<list>
</mainlist>
There can be multiple <para> tags.
Thank you for your help 
Last edited by sushil.sharma75 : November 18th, 2009 at 03:09 PM.
|

November 18th, 2009, 03:13 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 Posts
|
|
You will need to post a well-formed XML input sample first. Your current sample has a lot of opening <tag name="Number"> or <tag name="bullet"> but these are never closed.
__________________
Martin Honnen
Microsoft MVP - XML
My blog
|

November 18th, 2009, 03:16 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 Posts
|
|
And the output you posted isn't well-formed either, lots of <list> but no closing </list> at all.
__________________
Martin Honnen
Microsoft MVP - XML
My blog
|

November 18th, 2009, 03:20 PM
|
 |
Wrox Author
Points: 12,735, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
|
|
It looks as if you are trying to group adjacent paragraphs having the same tag name, which is very straightforward in XSLT 2.0 using <xsl:for-each-group group-adjacent=""/>. It's (much) more difficult in XSLT 1.0, but you will be able to find examples by googling for "positional grouping" or "sibling recursion".
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |