Jump to content

[solved] How To Extract Attributes From Xml File With Xsl File


subuntug

Recommended Posts

Hello;I have this XML file:

<?xml version="1.0" encoding="ISO-8859-1" ?>		<db>		<cities>			<city cityName="Abbecity" stateNumber="80"/>			<city cityName="Amiens" stateNumber="80"/>			<city cityName="Caen" stateNumber="14"/>			<city cityName="La Gacilly" stateNumber="56"/>			<city cityName="Rennes" stateNumber="35"/>			<city cityName="Saint-Germain en Laye" stateNumber="78"/>			<city cityName="Vannes" stateNumber="56"/>			<city cityName="Versailles" stateNumber="78"/>		</cities>		<states>			<state stateNumber="14" stateName="Calvados"/>			<state stateNumber="35" stateName="Ille-et-Vilaine"/>			<state stateNumber="59" stateName="Nord"/>			<state stateNumber="56" stateName="Morbihan"/>			<state stateNumber="78" stateName="Yvelines"/>			<state stateNumber="40" stateName="Landes"/>			<state stateNumber="80" stateName="Somme"/>			<state stateNumber="21" stateName="Côte d'or"/>		</states>	</db>

I want to extract each stateNumber attribute child of <states> element which isn't child of <cities> element. Which means, all states which don't have a city in my database.To do that I have to use XSL file.I tried a various of XSL combination but I failed to get what I want.Please help me to find a solution.Thanks in advance.

Link to comment
Share on other sites

You have not told us whether you want plain text or XML or HTML as the output so for simplicity I have choosen plain text. An XSLT 2.0 solution is as follows:

  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="2.0">    <xsl:output method="text"/>    <xsl:template match="/">	<xsl:value-of select="db/states/state[not(@stateNumber = /db/cities/city/@stateNumber)]/@stateNumber"/>  </xsl:template></xsl:stylesheet>

An XSLT 1.0 solution is as follows:

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">    <xsl:output method="text"/>    <xsl:template match="/">	<xsl:for-each select="db/states/state[not(@stateNumber = /db/cities/city/@stateNumber)]/@stateNumber">	  <xsl:value-of select="."/>	  <xsl:if test="position() != last()"><xsl:text> </xsl:text></xsl:if>	</xsl:for-each>  </xsl:template></xsl:stylesheet>

Both stylesheets, when run against your sample input, output "59 40 21".

Link to comment
Share on other sites

Thanks alot for your answer. You saved my life.But only the second solution works. The first solution didn't work because it gives me only the first match, in this case the number "59". I don't know why, may be I have to add for-each statement to perform a loop. In the second solution, which works fine, I found this statement :

<xsl:if test="position() != last()"><xsl:text> </xsl:text></xsl:if>

Can you explain briefly the meaning of this statement, I didn't understand it very well. Thank you very much for your help.

Link to comment
Share on other sites

The first solution does work but as stated you need to use it with an XSLT 2.0 processor. I list the XSLT 2.0 processors: Saxon 9, AltovaXML tools, Gestalt.As for the second solution, the XSLT 1.0 solution, that xsl:if instruction outputs a blank character between the attribute values. If we simply do <xsl:for-each select="..."><xsl:value-of select="."/></xsl:for-each> then the values output will not be separated. The xsl:if ensures that a blank character is output after each but the last value.

Link to comment
Share on other sites

Thanks alot for your quick answer; In fact I use the XSLT Prossessors built in the latest versions of firefox (3.5) and Internet Explorer 8. So is there any modern browser which take charge of XSLT 2.0 ? Because I don't know how to install specific processors like SAXON. I prefer use my web browsers to run my xml files. Thanks again

Link to comment
Share on other sites

So is there any modern browser which take charge of XSLT 2.0?
Unfrortunately, no.
Because I don't know how to install specific processors like SAXON. I prefer use my web browsers to run my xml files.
You install processors when you want to use them on the server (or embed them within a program), not when you want to use a program that has them. The people that made your browser also made an XSLT processor they embedded within that browser (IE has MSXML 3.0, FF has Transformiix, etc.), but if you were to make your own browser (or something...), you should install another XSLT processor OR make your own (if you had the time and nerves...).XSLT can be used in server side scripting languages like PHP for example, which uses libxslt (another XSLT 1.0 processor). Other languages, like ASP.NET and JSP can easily use a third party processor written in the same platform, such as SAXON. If you want to use SAXON in a "non native" environment, you'll need something like the library from my signature.
Link to comment
Share on other sites

In fact I use the XSLT Prossessors built in the latest versions of firefox (3.5) and Internet Explorer 8. So is there any modern browser which take charge of XSLT 2.0 ? Because I don't know how to install specific processors like SAXON. I prefer use my web browsers to run my xml files.
Current browsers only support XSLT 1.0.As for installing XSLT 2.0 processors, you have the choice between installing the command line versions like AltovaXML tools (runs on Windows) or like Saxon 9 (runs where Java 5 or later is installed or alternatively on Windows where .NET 2.0 or later is installed) or installing XML editors that come with these processors (e.g. Altova XML Spy comes with AltovaXML tools or Oxygen XML editor comes with Saxon).For Saxon there is also Kernow which provides a simple GUI to Saxon's XSLT and XQuery features and I think the web install will take care of installing Java too if that is not already available.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...