1

I am trying to transform one XML file to other XML file using XSLT file. Below is the source file:-

Source file

<country isocode="de" pk="8796093055010" uri="http://localhost:9001/ws410/rest/countries/de">
        <comments/>
        <creationtime>2011-08-03T21:53:35.624+05:30</creationtime>
        <dimVals/>
        <modifiedtime>2011-08-03T22:05:10.111+05:30</modifiedtime>
        <active>true</active>
        <name>Germany</name>
        <regions>
              <region isocode="DE-BW" pk="8796093055011" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-BW"/>
              <region isocode="DE-BY" pk="8796093087779" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-BY"/>
              <region isocode="DE-BE" pk="8796093120547" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-BE"/>
              <region isocode="DE-BR" pk="8796093153315" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-BR"/>
              <region isocode="DE-HB" pk="8796093186083" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-HB"/>
              <region isocode="DE-HH" pk="8796093218851" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-HH"/>
              <region isocode="DE-HE" pk="8796093251619" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-HE"/>
              <region isocode="DE-MV" pk="8796093284387" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-MV"/>
              <region isocode="DE-NI" pk="8796093317155" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-NI"/>
              <region isocode="DE-NW" pk="8796093349923" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-NW"/>
              <region isocode="DE-RP" pk="8796093382691" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-RP"/>
              <region isocode="DE-SL" pk="8796093415459" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-SL"/>
              <region isocode="DE-ST" pk="8796093448227" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-ST"/>
              <region isocode="DE-SN" pk="8796093480995" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-SN"/>
              <region isocode="DE-SH" pk="8796093513763" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-SH"/>
              <region isocode="DE-TH" pk="8796093546531" uri="http://localhost:9001/ws410/rest/countries/de/regions/DE-TH"/>
        </regions>
        <zones>
            <zone code="de" pk="8796093056179" uri="http://localhost:9001/ws410/rest/zones/de"/>
        </zones>
      </country>

XSLT file

<xsl:template match="/country/regions">
    <RECORDS> 
        <xsl:for-each select="region">
            <RECORD>
                <PROP NAME="isocode">
                    <PVAL><xsl:value-of select="@isocode"/></PVAL>
                </PROP>
                <PROP NAME="pk">
                    <PVAL><xsl:value-of select="@pk"/></PVAL>
                </PROP>
                <PROP NAME="uri">
                    <PVAL><xsl:value-of select="@uri"/></PVAL>
                </PROP>
            </RECORD>
        </xsl:for-each>
    </RECORDS>
</xsl:template>

Expected output

<RECORDS> 
        <RECORD>
            <PROP NAME="isocode">
                <PVAL>DE-BW</PVAL>
            </PROP>
            <PROP NAME="pk">
                <PVAL>8796093055011</PVAL>
            </PROP>
            <PROP NAME="uri">
                <PVAL>http://localhost:9001/ws410/rest/countries/de/regions/DE-BW</PVAL>
            </PROP>
        </RECORD>
        ...........
</RECORDS>

To check the output I have included <?xml-stylesheet type="text/xsl" href="<<XSLT file>>"?> and then I open the file on firefox browser. But the output looks like the following without the custom tags like <RECORDS>, <RECORD>, <PROP>

2011-08-03T21:53:35.624+05:30

        2011-08-03T22:05:10.111+05:30
        true
        Germany
        DE-BW8796093055011http://localhost:9001/ws410/rest/countries/de/regions/DE-BWDE-BY8796093087779http://localhost:9001/ws410/rest/countries/de/regions/DE-BYDE-BE8796093120547http://localhost:9001/ws410/rest/countries/de/regions/DE-BEDE-BR8796093153315http://localhost:9001/ws410/rest/countries/de/regions/DE-BRDE-HB8796093186083http://localhost:9001/ws410/rest/countries/de/regions/DE-HBDE-HH8796093218851http://localhost:9001/ws410/rest/countries/de/regions/DE-HHDE-HE8796093251619http://localhost:9001/ws410/rest/countries/de/regions/DE-HEDE-MV8796093284387http://localhost:9001/ws410/rest/countries/de/regions/DE-MVDE-NI8796093317155http://localhost:9001/ws410/rest/countries/de/regions/DE-NIDE-NW8796093349923http://localhost:9001/ws410/rest/countries/de/regions/DE-NWDE-RP8796093382691http://localhost:9001/ws410/rest/countries/de/regions/DE-RPDE-SL8796093415459http://localhost:9001/ws410/rest/countries/de/regions/DE-SLDE-ST8796093448227http://localhost:9001/ws410/rest/countries/de/regions/DE-STDE-SN8796093480995http://localhost:9001/ws410/rest/countries/de/regions/DE-SNDE-SH8796093513763http://localhost:9001/ws410/rest/countries/de/regions/DE-SHDE-TH8796093546531http://localhost:9001/ws410/rest/countries/de/regions/DE-TH

PS- When you add just words without tags they appear on web browser.

Edit

I am including my xsl file in my source file like



<?xml-stylesheet type="text/xsl" href="countries2.xsl"?>
    <?xml version="1.0" encoding="ISO-8859-1"?>


          <country isocode="de" pk="8796093055010" uri="http://localhost:9001/ws410/rest/countries/de">
and rest of the xml as source file

but when i am checking this on browser FF showing parsing error while on the IE i am getting following output

DE-BW8796093055011http://localhost:9001/ws410/rest/countries/de/regions/DE-BWDE-BY8796093087779http://localhost:9001/ws410/rest/countries/de/regions/DE-BYDE-BE8796093120547http://localhost:9001/ws410/rest/countries/de/regions/DE-BEDE-BR8796093153315http://localhost:9001/ws410/rest/countries/de/regions/DE-BRDE-HB8796093186083http://localhost:9001/ws410/rest/countries/de/regions/DE-HBDE-HH8796093218851http://localhost:9001/ws410/rest/countries/de/regions/DE-HHDE-HE8796093251619http://localhost:9001/ws410/rest/countries/de/regions/DE-HEDE-MV8796093284387http://localhost:9001/ws410/rest/countries/de/regions/DE-MVDE-NI8796093317155http://localhost:9001/ws410/rest/countries/de/regions/DE-NIDE-NW8796093349923http://localhost:9001/ws410/rest/countries/de/regions/DE-NWDE-RP8796093382691http://localhost:9001/ws410/rest/countries/de/regions/DE-RPDE-SL8796093415459http://localhost:9001/ws410/rest/countries/de/regions/DE-SLDE-ST8796093448227http://localhost:9001/ws410/rest/countries/de/regions/DE-STDE-SN8796093480995http://localhost:9001/ws410/rest/countries/de/regions/DE-SNDE-SH8796093513763http://localhost:9001/ws410/rest/countries/de/regions/DE-SHDE-TH8796093546531http://localhost:9001/ws410/rest/countries/de/regions/DE-TH

output on IE is without tags.i am wondering is including xsl in my source xml can show me the correct output after tranformation.

2
  • This question is tagged XSLT 2.0. I would try to set the stylesheet to 1.0 as is well known that browsers does not support XSLT 2.0. Commented Sep 2, 2011 at 7:01
  • 1
    The first thing I would try is switching the order of your <?xml version="1.0" encoding="ISO-8859-1"?> and <?xml-stylesheet type="text/xsl" href="countries2.xsl"?> in the XML source file. The <?xml version="1.0" encoding="ISO-8859-1"?> should always be first. Commented Sep 2, 2011 at 16:49

2 Answers 2

2

The problem isn't with your XSLT code, it is with the way you are running (or rather, not running) the transformation. It's not entirely clear to me how you are trying to do this, but basically the XSLT is never executed so you're seeing the raw text content of your source document.

Sign up to request clarification or add additional context in comments.

Comments

1

This is one XSLT that runs:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <xsl:apply-templates select="/country/regions"/>
  </xsl:template>
  <xsl:template match="regions">
    <RECORDS>
      <xsl:for-each select="region">
        <RECORD>
          <PROP NAME="isocode">
            <PVAL>
              <xsl:value-of select="@isocode"/>
            </PVAL>
          </PROP>
          <PROP NAME="pk">
            <PVAL>
              <xsl:value-of select="@pk"/>
            </PVAL>
          </PROP>
          <PROP NAME="uri">
            <PVAL>
              <xsl:value-of select="@uri"/>
            </PVAL>
          </PROP>
        </RECORD>
      </xsl:for-each>
    </RECORDS>
  </xsl:template>
</xsl:stylesheet>

2 Comments

Please don't use version="1.1" in a stylesheet. 1.1 was a working draft that never made it to Recommendation.
As far as IE9 is concerned, I believe it is the way it works. Your last edit clearly shows that the XSLT is executed - otherwise, the first text in your output should've been 2011-08-03T21:53:35.624+05:30 instead of DE-BW8796093055011. It is simply a matter of how that is being displayed by IE9. I played with the developer tools in IE 9 and switching compatibility modes while watching the results is definitely helping to understand what is going on. I do not have Firefox, but I will look into that later on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.