Link to home
Start Free TrialLog in
Avatar of ethar turky
ethar turkyFlag for Saudi Arabia

asked on

Need to write xsl to extract xml[schema] from another xsl :)

Greetings,
 I am trying to extract  [as much as possible ] XML schema from XSL file. ( actually I need to get the xml structure )
The XSL ( in my case it is a source file)
<?xml version="1.0" encoding="windows-1256"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html"/>
	<xsl:template match="/">
		<xsl:element name="div">			
			<xsl:apply-templates select="ROOT/item"/>
		</xsl:element>
	</xsl:template>
	<xsl:template match="item">			
			<xsl:element name="div">				
				<xsl:element name="div">				
					<xsl:element name="div">						
						<xsl:element name="h3">
							<xsl:attribute name="class"><xsl:value-of select="Font"/></xsl:attribute>
							<xsl:element name="span">								
								<xsl:attribute name="data-value"><xsl:value-of select="TOTALPROFIT"/></xsl:attribute>
								<xsl:value-of select="TOTALPROFIT"/>
							</xsl:element>
							<xsl:element name="small">
								<xsl:attribute name="class"><xsl:value-of select="Font"/></xsl:attribute>$</xsl:element>
						</xsl:element>
						<xsl:element name="small">
							<xsl:value-of select="Caption"/>
						</xsl:element>
					</xsl:element>
					<xsl:element name="div">						
						<xsl:element name="i">
							<xsl:attribute name="class"><xsl:value-of select="Icon"/></xsl:attribute>
						</xsl:element>
					</xsl:element>
				</xsl:element>
				<xsl:element name="div">					
					<xsl:element name="div">
						<xsl:element name="span">
							<xsl:attribute name="class">progress-bar progress-bar-success <xsl:value-of select="ProgressBarStyle"/></xsl:attribute>
							<xsl:attribute name="style">width: <xsl:value-of select="Percentage"/>%;</xsl:attribute>
							<xsl:element name="span">							
								<xsl:value-of select="Percentage"/>% progress</xsl:element>
						</xsl:element>
					</xsl:element>
					<xsl:element name="div">						
						<xsl:element name="div">							
							<xsl:value-of select="Percentage"/>%</xsl:element>
					</xsl:element>
				</xsl:element>
			</xsl:element>
	</xsl:template>
</xsl:stylesheet>

Open in new window


and need to extract the following xml :
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="ROOT">
		<xs:complexType>
			<xs:sequence>
				<xs:element maxOccurs="unbounded" name="item">
					<xs:complexType>
						<xs:sequence>
							<xs:element name="Caption" type="xs:string" />							
							<xs:element name="TOTALPROFIT" type="xs:unsignedShort" />
							<xs:element name="Percentage" type="xs:unsignedByte" />
							<xs:element name="Icon" type="xs:string" />
							<xs:element name="Font" type="xs:string" />
							<xs:element name="ProgressBarStyle" type="xs:string" />
						</xs:sequence>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

Open in new window


or any other xml format that describe the xsl file

[Yes I can edit the source XSL to help generating the XML]
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

First, your question is really vague (way too vague to get a uefull answer actually)

Deriving a schema for the expected result XML from the XSLT that generates it is really tricky.
If the @select in the apply templates are very explicit, you could. But most of processing an XSLT is about the runtime behaviour (the source data directs the process) So there is usualy not enough information at static compilation time. Chances you get a complete schema out of your static interpretation are small. Note that full interpretation of a stylesheet is hard because the priority rules are complex

If you are looking for the schema of the result files, I would suggest to execute the XSLT on a bunch of source files and derive a schema from the set of result files using trang: http://www.thaiopensource.com/relaxng/trang.html
It is a common approach to validate completeness of your processes whilst you develop them (some sort of test driven development) If that at all is what you want to do

If you are simply looking for technical assistance on trasforming an xsl file into a schema file using xslt...
- just make sure you have the namespaces right
- use keys to get from an apply templates to the  actual templates (though this would be rough guessing)
You might also bump into XPath parsing... tricky

As long as you don't explain what exactly you want to achieve, this is all I can do
Avatar of ethar turky

ASKER

Dear Geert Bormans, Thanks for your explanation .
First of all ( and as mentioned ) I  rule the XSL files ( the source file ) I can restructure it to make the job easier.

Purposes of doing so, to transform XSL against XML files which we had to build it.
We got 100's XSL files and need to test it.

I need to automate building "XML files" process as possible and reduce human errors.
in worst scenario we need to collect as much data as possible from the XSL to imagine the XML structure,

(I can pay for this job.)
sounds a bit weird
- you can restructure the XSL
- but you have 100 of those to deal with

I still miss the actual goal. What is the mapping between xsl and xml schema?
We can program to get what you need from what you have, but thwt would be something we can not do for 100 xslts
I understand you need something to validate you xslt development. If that is the case, think about the test driven approach on the result, not on the code

I need more details, really

About payment, well I can do this as a contractor... or assit you in doing it your self through this forum, pretty much depends on the quantity of the work
I am not sure that I made myself clear enough, here I try

When someone asks me, "can you generate a schema" my first question should be "why?" "What do you want to achieve with the schema?" I have not seen that question answered

If the answer is "code coverage" (see whether the XSLT does what it needs to) there are a zillion of better techniques to achieve that

Let us first look at what you want to do with the schema. A schema is for validating something. What do you need to validate?

If none of my proposals at the end are satisfactory for you, we wil do the schema generation. For now, I advice you to explore the other options first
A legacy system needed to be reproduced , the only resources we have is the XSL.
The schema will help us to utilized the XSL and rescue a lot of efforts of analysis etc...+ give us a better look at backend operations,
Actually the changing of XSL is part of our job to achieve some other goals.

Also the XML schema is needed to store the legacy data that will save us a lot of efforts.

its not mater of "code coverage" [ONLY], Yes its a part of the job ,  you may referee  to some resources to do  "code coverage".

I would like to generate a full informed schema but if not possible to achieve such a goal , we are open to be more compromising , taking in account the ability of changing the XSL files to facilitate the coverage process,.

Here's a small example of "XML schema generator" for one of the XSL files we got:
<xsl:stylesheet  version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml"/>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
	 
	<xsl:template match="/">
						<![CDATA[<item>]]><xsl:apply-templates select="." /><![CDATA[</item>]]>
	</xsl:template>
<xsl:template match="Blocks" name="Blocks" >
		<![CDATA[<item>]]><xsl:apply-templates select="Attributes[@DataSourceType='XML']"  /><![CDATA[</item>]]>
		<xsl:for-each select="Contents"><![CDATA[<item>]]><xsl:call-template name="Blocks"  /><![CDATA[</item>]]></xsl:for-each>
</xsl:template> 
<xsl:template match="Attributes"  ><![CDATA[<item>]]>   <xsl:value-of select="@ValueField"/><![CDATA[</item>]]> </xsl:template>
</xsl:stylesheet>

Open in new window

Sorry to say but you are loosing me.
The XSLT you sent is not generating a XSD schema

Please, I can accept that you need to remain vague, but if you want some help, you need to be more precise

So the schema is needed to store the legacy XML? We really need more precise info? Is the schema for source data? Is the schema for result data? Do you have data files available?
I don't meant to be vague by any means, just ask any question and I will provide the answer.

Yes the XSLT I sent doesn't generate the XSD schema , and the XSD schema is my high ambition.

for now I need to generate a simple XML file witch can be run against (same ) XSL file.

I can accept partial xml file, or not completed one , or even xml file needed to be edit before using it.

The data has many resources , some in excel , more in text and some in database( db3 ).
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial