p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > XML > XSLT
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 4th, 2009, 10:52 AM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 15%
Activity: 15% Activity: 15% Activity: 15%
 
Join Date: Nov 2009
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old November 4th, 2009, 11:19 AM
mhkay's Avatar
Wrox Author
Points: 12,735, Level: 48
Points: 12,735, Level: 48 Points: 12,735, Level: 48 Points: 12,735, Level: 48
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old November 5th, 2009, 12:10 AM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 15%
Activity: 15% Activity: 15% Activity: 15%
 
Join Date: Nov 2009
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default

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 View Post
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>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old November 5th, 2009, 05:13 AM
mhkay's Avatar
Wrox Author
Points: 12,735, Level: 48
Points: 12,735, Level: 48 Points: 12,735, Level: 48 Points: 12,735, Level: 48
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old November 5th, 2009, 09:46 AM
Friend of Wrox
Points: 3,131, Level: 23
Points: 3,131, Level: 23 Points: 3,131, Level: 23 Points: 3,131, Level: 23
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 Posts
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
The Following User Says Thank You to Martin Honnen For This Useful Post:
sushil.sharma75 (November 5th, 2009)
  #6 (permalink)  
Old November 5th, 2009, 11:57 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 15%
Activity: 15% Activity: 15% Activity: 15%
 
Join Date: Nov 2009
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thank you very much for your post. Its really helped to solved my problems.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #7 (permalink)  
Old November 18th, 2009, 03:05 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 15%
Activity: 15% Activity: 15% Activity: 15%
 
Join Date: Nov 2009
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #8 (permalink)  
Old November 18th, 2009, 03:13 PM
Friend of Wrox
Points: 3,131, Level: 23
Points: 3,131, Level: 23 Points: 3,131, Level: 23 Points: 3,131, Level: 23
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 Posts
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #9 (permalink)  
Old November 18th, 2009, 03:16 PM
Friend of Wrox
Points: 3,131, Level: 23
Points: 3,131, Level: 23 Points: 3,131, Level: 23 Points: 3,131, Level: 23
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 Posts
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #10 (permalink)  
Old November 18th, 2009, 03:20 PM
mhkay's Avatar
Wrox Author
Points: 12,735, Level: 48
Points: 12,735, Level: 48 Points: 12,735, Level: 48 Points: 12,735, Level: 48
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed to convert an XML file rpalmer68 XSLT 3 June 21st, 2009 12:45 AM
Subject: Convert XML using XSLT manoj1984 XSLT 6 May 30th, 2008 05:27 PM
Using XSLT to convert XML to a table ? nobitavn94 XSLT 3 October 30th, 2006 11:03 AM
merge two xml file and make new xml using xslt ketan XSLT 0 September 21st, 2004 09:48 AM
Merge XML files into a xml file using xslt lxu XML 4 November 6th, 2003 06:01 PM



All times are GMT -4. The time now is 11:14 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc