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 November 4th, 2009, 10:52 AM
Registered User
 
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
 
Old November 4th, 2009, 11:19 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
 
Old November 5th, 2009, 12:10 AM
Registered User
 
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>
 
Old November 5th, 2009, 05:13 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
 
Old November 5th, 2009, 09:46 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 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, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
sushil.sharma75 (November 5th, 2009)
 
Old November 5th, 2009, 11:57 PM
Registered User
 
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.
 
Old November 18th, 2009, 03:05 PM
Registered User
 
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..
 
Old November 18th, 2009, 03:13 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 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, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old November 18th, 2009, 03:16 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 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, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old November 18th, 2009, 03:20 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed to convert an XML file rpalmer68 XSLT 3 June 20th, 2009 11:45 PM
Subject: Convert XML using XSLT manoj1984 XSLT 6 May 30th, 2008 04: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 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.