onurelibol
onurelibol

Reputation: 777

XML to XML transformation with XSL

I'm trying to transform an XML file to another XML file. Here is my input file:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="inlamning.xsl"?>
<Ansokan>

  <forskola_namn>Kopparholmen</forskola_namn>

  <barn persunnummer="200505051111">
    <namn>Niklas Niklasson</namn>
    <syskon_pnr>200606061111</syskon_pnr>
    <allergi>Laktos</allergi>
  </barn>

  <vardnadshavare personnummer="198606061111">
    <fornamn>Magnus</fornamn>
    <efternamn>Niklasson</efternamn>
    <adress>
      <hem>Magnusgatan 10</hem>
      <jobb>Jobbgatan 1</jobb>
    </adress>
    <telefon>
      <hem>0520-12345</hem>
      <mobil>077-731731</mobil>
    </telefon>
  </vardnadshavare>

  <vardnadshavare personnummer="198605051111">
    <fornamn>Hanna</fornamn>
    <efternamn>Niklasson</efternamn>
    <adress>
      <hem>Magnusgatan 10</hem>
      <jobb>Jobbgatan 5</jobb>
    </adress>
    <telefon>
      <hem></hem>
      <mobil>077-721721</mobil>
    </telefon>
  </vardnadshavare>

  <annat_ommande_skal>Inget</annat_ommande_skal>

</Ansokan>

And this is the transformated XML I try to do:

<begaranominkomstuppgift>
   <person personnummer="">
      <fornamn></fornamn>
      <efternamn></efternamn>
   </person>
   <person personnummer="">
      <fornamn></fornamn>
      <efternamn></efternamn>
   </person>
</begaranominkomstuppgift>

What I'm trying to do is, creating a new root as <begaranominkomstuppgift> and taking 2 vardnadshavare as <person> with the attribute, and taking just vardnadshavare <fornamn> and <efternamn>.

And this is my XSL file:

<?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" version="1.0" indent="yes" />

        <xsl:template match="/">
            <begaranominkomstuppgift>
                <xsl:apply-templates select="Ansokan/vardnadshavare" />
            </begaranominkomstuppgift>
        </xsl:template>

        <xsl:template match="Ansokan/vardnadshavare">
            <xsl:element name="person" personnummer="{@personnummer}">
                <xsl:element name="fornamn">
                    <xsl:value‐of select="fornamn" />
                </xsl:element>
                <xsl:element name="efternamn">
                    <xsl:value‐of select="efternamn" />
                </xsl:element>
        </xsl:template>
    </xsl:stylesheet>

XSL file is probably not taking the right information. And moreover after transformation I can't see the tags. I mean even if I choose the right information, it's not showing xml tags after the transformation. Where am I going wrong?

Upvotes: 1

Views: 2062

Answers (1)

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22647

I think what you would like to do is the following. The most significant differences between your solution and mine:

  • if you know what the name of an element is going to be, there is no need for <xsl:element name="person">, simply use <person>
  • inside the new person, I use copy-of to copy the two elements

XSLT Stylesheet

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output method="xml" omit-xml-declaration="no" encoding="UTF-8" indent="yes" />
    <xsl:strip-space elements="*"/>

    <xsl:template match="/Ansokan">
        <begaranominkomstuppgift>
            <xsl:apply-templates/>
        </begaranominkomstuppgift>
    </xsl:template>

    <xsl:template match="vardnadshavare">
        <person personnummer="{@personnummer}">
            <xsl:copy-of select="fornamn | efternamn"/>
        </person>
    </xsl:template>

    <xsl:template match="text()"/>

</xsl:transform>

XML Output

<?xml version="1.0" encoding="UTF-8"?>
<begaranominkomstuppgift>
   <person personnummer="198606061111">
      <fornamn>Magnus</fornamn>
      <efternamn>Niklasson</efternamn>
   </person>
   <person personnummer="198605051111">
      <fornamn>Hanna</fornamn>
      <efternamn>Niklasson</efternamn>
   </person>
</begaranominkomstuppgift>

Try this solution online here.


XSL file is probably not taking the right information. And moreover after transformation I cant see the tags. I mean even if I choose the right information, its not showing xml tags after the transformation. Where am I doing wrong?

The problems with your original XSLT code are that 1) one of the xsl:element instructions is not closed and 2) personnummer cannot be an attribute of xsl:element. You would have to use xsl:attribute:

<xsl:element name="person">
  <xsl:attribute name="personnummer" select="@personnummer"/>

Also, the hyphen characters you have used between value and of are somehow not recognized by my processor.

But usually, if a transformation is "not showing tags" it's because of a namespace. Chances are that your input document has a default namespace that you have not shown to us.

Upvotes: 2

Related Questions