XML Development

Moderators: None (Apply to moderate this forum)
Number of threads: 257
Number of posts: 456

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Can this be done with XSLT? Posted by jiggs786 on 30 Jan 2009 at 10:01 AM
Hi Everyone,

I've been stuck on a XSLT problem trying to convert an XML doc to another XML and now I'm wondering if it's even possible to be done with XSTL. Here is a very simple breakdown of what I'm trying to do:

Convert this XML:

<Request>
   <SomeTypeA>
       <Variable>A</Variable>
   </SomeTypeA>
    <SomeTypeB>
       <Variable>B</Variable>
   </SomeTypeB>
   <SomeTypeB>
       <Variable>B</Variable>
   </SomeTypeB>
</Request>


To:

<Group name = Request>
   <Group name = SomeTypeA>
       <Variable>A</Variable>
   </Group>
   <Group name = SomeTypeB>
      <Item>
           <Variable>A</Variable>
      </Item>
      <Item>
           <Variable>A</Variable>
      </Item>
    </Group>
</Group>


The key here is I want to group all Nodes under <Request> that have the same name, in the example above <SomeTypeB> and process them together so that they belong to the same <Group name = SomeTypeB> after the conversion and they are separate by the new <Item> tags.

So far everything I tried has failed, the closest I get is that I get two sets <Group name = SomeTypeB> tags which is incorrect for my needs.

Can this even be done with XSLT or do I have to use Java or something else?

Much appreciated if you could solve this!!

Thanks

Report
Re: Can this be done with XSLT? Posted by xml_looser on 27 Mar 2009 at 8:17 AM
yes it is posible
for testiing new xml data
<?xml version="1.0"?>
<Request>
	<SomeTypeA>
		<Variable>A</Variable>
	</SomeTypeA>
	<SomeTypeB>
		<Variable>B1</Variable>
	</SomeTypeB>
	<SomeTypeB>
		<Variable>B2</Variable>
	</SomeTypeB>
	<SomeTypeC>
		<Variable>C3</Variable>
	</SomeTypeC>
	<SomeTypeB>
		<Variable>B3</Variable>
	</SomeTypeB>
	<SomeTypeB>
		<Variable>B4</Variable>
	</SomeTypeB>
	<SomeTypeQ>
		<Variable>Q1</Variable>
	</SomeTypeQ>
	<SomeTypeQ>
		<Variable>Q2</Variable>
	</SomeTypeQ>
	<SomeTypeQ>
		<Variable>Q3</Variable>
	</SomeTypeQ>
	<SomeTypeQ>
		<Variable>Q4</Variable>
	</SomeTypeQ>
</Request>






<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes"/>
	<xsl:key name="wieviel" match="//Request/*" use="local-name()"/>

	<xsl:template match="/Request">
		<xsl:call-template name="go">
			<xsl:with-param name="all" select="*"/>
		</xsl:call-template>
	</xsl:template>
	<xsl:template name="go">
		<xsl:param name="all"/>

		<xsl:element name="Group">
			<xsl:attribute name="name">
				<xsl:value-of select="local-name()"/>
			</xsl:attribute>
			<xsl:for-each select="$all[generate-id()= generate-id(key('wieviel', local-name())[1])]">
				<xsl:sort select="local-name()" order="ascending" data-type="text"/>

				<xsl:variable name="name" select="local-name()"/>
				<xsl:element name="group">
					<xsl:attribute name="name">
						<xsl:value-of select="local-name()"/>
					</xsl:attribute>
					<xsl:choose>
						<xsl:when test="1 != count(key('wieviel',local-name()))">
							<xsl:call-template name="counter">
								<xsl:with-param name="vari" select="count(key('wieviel',local-name()))"/>
								<xsl:with-param name="all" select="$all"/>
								<xsl:with-param name="kname" select="local-name()"/>
							</xsl:call-template>
						</xsl:when>
						<xsl:otherwise>
							<xsl:element name="Variable">
								<xsl:value-of select="./Variable"/>
							</xsl:element>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:element>
			</xsl:for-each>
		</xsl:element>
	</xsl:template>
	<xsl:template name="counter">
		<xsl:param name="vari"/>
		<xsl:param name="aktu" select="1"/>
		<xsl:param name="all"/>
		<xsl:param name="kname"/>
		<xsl:element name="Item">
			<xsl:element name="Variable">
				
				<xsl:value-of select="$all[local-name()=$kname][$aktu]/*"/>
			</xsl:element>
		</xsl:element>
		<xsl:if test="$aktu &lt; $vari">
			<xsl:call-template name="counter">
				<xsl:with-param name="vari" select="count(key('wieviel',local-name()))"/>
				<xsl:with-param name="aktu" select="$aktu + 1"/>
				<xsl:with-param name="all" select="$all"/>
				<xsl:with-param name="kname" select="$kname"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>




result

<?xml version='1.0' ?>
<Group name="Request">
  <group name="SomeTypeA">
    <Variable>A</Variable>
  </group>
  <group name="SomeTypeB">
    <Item>
      <Variable>B1</Variable>
    </Item>
    <Item>
      <Variable>B2</Variable>
    </Item>
    <Item>
      <Variable>B3</Variable>
    </Item>
    <Item>
      <Variable>B4</Variable>
    </Item>
  </group>
  <group name="SomeTypeC">
    <Variable>C3</Variable>
  </group>
  <group name="SomeTypeQ">
    <Item>
      <Variable>Q1</Variable>
    </Item>
    <Item>
      <Variable>Q2</Variable>
    </Item>
    <Item>
      <Variable>Q3</Variable>
    </Item>
    <Item>
      <Variable>Q4</Variable>
    </Item>
  </group>
</Group>



Helmut Hagemann Germany
xml_user@web.de
Report
import xml file in indesign Posted by akmeeran on 2 Oct 2009 at 12:40 PM
my xml file like this....
<dataroot>
<BU_x0020_alpha_x0020_08>
<Classification>Agricultural &amp; Horticultural Contractors</Classification>
<Customer>Arid Land Dev Co LLC </Customer>
<City>Abu Dhabi </City>
<Box>5294</Box>
<Tel_x0020_No>02 6319244</Tel_x0020_No>
<Fax_x0020_No>02 6318366</Fax_x0020_No>
<Email>aridland@emirates.net.ae</Email>
<Website>www.aridland-dev.com</Website>
<image>logo1.eps</image>
</BU_x0020_alpha_x0020_08>
</dataroot>


my xslt like this...

<?xml version="1.0"?>
<!--master.xsl-->
<xsl:stylesheet version="1.0"
xmlns:xs="http://www.w3.org/2001/XML Schema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="master sub.xsl"/>
<xsl:output method="xml" indent="no" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>


<xsl:template match="/dataroot">
<xsl:apply-templates>
<xsl:sort select="Classification[1]" order="ascending"/>
<xsl:sort select="Customer[1]" order="ascending" />
</xsl:apply-templates>
</xsl:template>


<xsl:template match="BU_x0020_alpha_x0020_08">
<Classification>
<xsl:if test="Classification!=preceding-sibling::BU_x0020_alpha_x0020_08[1]/Classification">
<xsl:value-of select="Classification"/>
<xsl:apply-templates select="/BU_x0020_alpha_x0020_08/Classification[1]">
<xsl:sort/>
</xsl:apply-templates>
<xsl:text>&#xA;</xsl:text>
</xsl:if>
</Classification>
<Customer>
<xsl:if test="Customer!=preceding-sibling::BU_x0020_alpha_x0020_08/Customer">
<xsl:value-of select="Customer"/>
<xsl:apply-templates select="/BU_x0020_alpha_x0020_08/Customer">
<xsl:sort/>
</xsl:apply-templates>
<xsl:text>&#xA;</xsl:text>
</xsl:if>
</Customer>
<Address>
<xsl:if test="Box">
<xsl:value-of select="Box"/>
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:if test="City">
<xsl:value-of select="City"/>
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:if test="Tel_x0020_No">
<xsl:text>Tel:</xsl:text>
<xsl:text>&#x9;</xsl:text>
<xsl:value-of select="Tel_x0020_No"/>
<xsl:text>&#xA;</xsl:text>
</xsl:if>
</Address>
<Fax>
<xsl:if test="Fax_x0020_No">
<xsl:text>Fax: </xsl:text>
<xsl:text>&#x9;</xsl:text>
<xsl:value-of select="Fax_x0020_No"/>
<xsl:text>&#xA;</xsl:text>
</xsl:if>
</Fax>
<Email>
<xsl:if test="Email">
<xsl:text>email:</xsl:text>
<xsl:value-of select="Email"/>
<xsl:text>&#xA;</xsl:text>
<xsl:text></xsl:text>
</xsl:if>
</Email>
<Website>
<xsl:if test="Website">
<xsl:value-of select="Website"/>
<xsl:text>&#xA;</xsl:text>
</xsl:if>
</Website>
<image>
<xsl:if test="image">
<xsl:value-of select="image"/>
</xsl:if>
</image>
</xsl:template>
</xsl:stylesheet>


but problem is EPS image cannot coming with text.
please help me....



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.