xml - Issues removing schema and namespace with XSLT -


i'm trying parse environment of canada's daily climate data, attribute centric element centric style, such can import result access database.

i've got working way want it, requires me manually cut out

<climatedata **xmlns:xsd="dummylink" xsd:schemalocation="dummylink.xsd"**> 

in order work.

i've added code xslt remove namespace root element in document, when this, ends changing way 1 of tag's transform. meaning station data tag no longer has it's attributes formatted elements.

here xml (after xslt), shortened see first day of element data. can see removed xmlns:xsd="dummylink" , xsd prefix schemalocation, left schemalocation , incorrectly formats stationdata.

<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="envcanstack.xsl"?>  <climatedata schemalocation="http://www.climate.weatheroffice.gc.ca/climatedata/bulkxml/bulkschema.xsd">  <lang>eng</lang>  <stationinformation>  <name>oshawa wpcp</name> <province>ontario</province> <latitude>43.87</latitude> <longitude>-78.83</longitude> <elevation>83.80</elevation> <climate_identifier>6155878</climate_identifier> <wmo_identifier></wmo_identifier> <tc_identifier></tc_identifier>  </stationinformation>  <stationdata day="1" month="1" year="2017">  <maxtemp>4.0</maxtemp> <mintemp>0.0</mintemp> <meantemp>2.0</meantemp>16.00.0<totalrain>0.0</totalrain> <totalsnow>0.0</totalsnow> <totalprecipitation>0.0</totalprecipitation> <snowonground>5</snowonground>  </stationdata>  </climatedata> 

and here xslt

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"     xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">   <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>   <xsl:strip-space elements="*"/>    <!-- default, elements , text nodes copied,        , elements' attributes , contents transformed child nodes        of output element -->   <xsl:template match="node()">     <xsl:copy>       <xsl:apply-templates select="@* | node()"/>     </xsl:copy>   </xsl:template>    <!-- default, attributes transformed elements -->   <xsl:template match="@*">     <xsl:element name="{name()}">       <xsl:value-of select="."/>     </xsl:element>   </xsl:template>    <!-- elements have contents transformed -->   <xsl:template match="dirofmaxgust | speedofmaxgust | heatdegdays | cooldegdays">     <!-- no xsl:copy, , attribute children, if any, ignored -->     <xsl:apply-templates select="@* | node()"/>   </xsl:template>    <!-- removes specific attributes (@) removed. blank template body means         "transformation" ignore them or re print them blank --> <xsl:template match="@description|@units|@quality"/>  <!-- i'm trying remove namespace/schemalocation -->   <xsl:template match="*">     <xsl:element name="{local-name(.)}">       <xsl:apply-templates select="@* | node()"/>     </xsl:element>   </xsl:template>    <xsl:template match="@*">     <xsl:attribute name="{local-name(.)}">       <xsl:value-of select="."/>     </xsl:attribute>   </xsl:template>  </xsl:stylesheet> 

just reference, here xml looks when manually cut namespace , don't include code me.

<?xml-stylesheet type="text/xsl" href="envcanstack.xsl"?>  <climatedata>  <lang>eng</lang>  <stationinformation>  <name>oshawa wpcp</name> <province>ontario</province> <latitude>43.87</latitude> <longitude>-78.83</longitude> <elevation>83.80</elevation> <climate_identifier>6155878</climate_identifier> <wmo_identifier> </wmo_identifier> <tc_identifier> </tc_identifier>  </stationinformation>  <stationdata>  <day>1</day> <month>1</month> <year>2017</year> <maxtemp>4.0</maxtemp> <mintemp>0.0</mintemp> <meantemp>2.0</meantemp> <totalrain>0.0</totalrain> <totalsnow>0.0</totalsnow> <totalprecipitation>0.0</totalprecipitation> <snowonground>5</snowonground>  </stationdata>  </climatedata> 

before hate on how i've set i'm student programmer on placement , i'm extremely new xml/xslt advice/insight/explanation why happening , might able fix appreciated.

you have conflicting template rules different priorities. take @ overview of how xslt priority works http://lenzconsulting.com/how-xslt-works/#priority

remove following template rule should work

<!-- default, attributes transformed elements --> <xsl:template match="@*">   <xsl:element name="{name()}">     <xsl:value-of select="."/>   </xsl:element>   </xsl:template> </xsl:template> 

and replace attribute template @ end below

  <xsl:template match="@*"> <xsl:choose>     <xsl:when test="local-name(.)='schemalocation'">       <xsl:attribute name="{local-name(.)}">         <xsl:value-of select="."/>       </xsl:attribute>     </xsl:when>     <xsl:otherwise>         <xsl:element name="{name()}">          <xsl:value-of select="."/>         </xsl:element>     </xsl:otherwise>   </xsl:choose> 


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -