Jump to content

Using xml sheme to transform xml 2 xml


Guest martensnl

Recommended Posts

Guest martensnl

Hi,I want to transform a xml file using an xml schema. This is the xml i have:

<?xml version="1.0" encoding="iso-8859-1"?><?xml-stylesheet type="text/xsl" href="C:\a_test.xsl"?><tif>	<posttyp typ="Z">  <kornnr>1320</kornnr>  <filnamn>1320_KA.totxml</filnamn>  <datum>20060131</datum>  <klockslag>00:51:43</klockslag>	</posttyp>	<posttyp typ="A">  <expimp typ="E"> 	 <goods_nom_item id="01011010" status="N">    <dat_start>20020101</dat_start>    <dat_end/> 	 </goods_nom_item> 	 <goods_nom_item id="01019011" status="N">    <dat_start>20020101</dat_start>    <dat_end/> 	 </goods_nom_item>  </expimp>  <expimp typ="I"> 	 <goods_nom_item id="0101101000" status="N">    <dat_start>20020101</dat_start>    <dat_end/> 	 </goods_nom_item> 	  	 <goods_nom_item id="0101191000" status="N">    <dat_start>19720101</dat_start>    <dat_end>20011231</dat_end> 	 </goods_nom_item>  </expimp>	</posttyp></tif>

And this is the structure i want the new xml file to look like:

<goods_nom_item>	<id></id>  (id of goods_nom_item)	<expimp></expimp>	(type of expimp)	<dat_start></dat_start>	<dat_end></dat_end></goods_nom_item>

Currently this is my schema but it doesnot work:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"><xsl:template match="/">	<xsl:for-each select="//expimp">    <xsl:for-each select="//goods_nom_item"> 	 <xsl:value-of select="expimp[@typ]"/> 	  	 <xsl:value-of select="@id"/> 	 <xsl:value-of select="//dat_start"/> 	 <xsl:value-of select="//dat_end"/>  </xsl:for-each>	</xsl:for-each></xsl:template></xsl:stylesheet>

Does anyone have any idea how the schema must look like?

Link to comment
Share on other sites

Execuse me for being so honest, but: hahaha :) . I never though I would see someone confusing Schema with XSLT. I mean, the overall fact that you've written something and use the XSL namespace should tell you at least that :) .Schema is a way to define legal building blocks of XML documents.XSLT is a way to transform XML documents to other documets from the XML family, another XML or just a plain text.Anyway... back to the problemYour XSLT actually worked when I tested it. The only adjustment I made was the reference to the XSLT file, because I stored the XSLT in the same folder as the XML, so I didn't needed the "C:\". Try this for starters...I also notice that you want XML output, but you haven't pointed any XML tags to use. XSLT uses XPath expression that extract the content of the desired element or attribute. This means that you must "manually" write the names of the new XML elements and attributes.Try this XSLT:

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml" encoding="utf-8" indent="yes" version="1.0"/><xsl:template match="/"><goods_nom_item>	<xsl:for-each select="//goods_nom_item">  <id><xsl:value-of select="@id"/></id>  <expimp><xsl:value-of select="expimp[@typ]"/></expimp>  <dat_start><xsl:value-of select="//dat_start"/></dat_start>  <dat_end><xsl:value-of select="//dat_end"/></dat_end>	</xsl:for-each></goods_nom_item></xsl:template></xsl:stylesheet>

My tests display the output as a text though. There isn't any XML tree shown. I haven't tryed converting XML-to-XML so I don't know if that's how it's suppose to be.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...