简体   繁体   中英

Transforming XML to another XML with XSLT

I'm trying to transform an XML file into another XML using XSLT, but all I get is a blank page.

I'm trying to transform this XML

 <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="xslt.xslt"?> <ForskoleAnsokan> <Vardnadshavare> <Vardtagare> <Fornamn>Arne</Fornamn> <Efternamn>Jonsson</Efternamn> <Personnummer>8802202220</Personnummer> <Adress>Galavägen 13</Adress> <Postnummer>46444</Postnummer> <Postort>Trandhatan</Postort> <Kontakt>072283832</Kontakt> </Vardtagare> <Vardtagare> <Fornamn>Gunilla</Fornamn> <Efternamn>Jonsson</Efternamn> <Personnummer>8702102110</Personnummer> <Adress>Galavägen 13</Adress> <Postnummer>46444</Postnummer> <Postort>Trandhatan</Postort> <Kontakt>073262613</Kontakt> </Vardtagare> </Vardnadshavare> </ForskoleAnsokan> 

Using this 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"/> <xsl:template match="/"> <begaranominkomstuppgift> <xsl:for-each select="ForskoleAnsokan/Vardnadshavare/Vardtagare"> <person> <xsl:attribute name="Personnummer"> <xsl:value-of select="@Personnummer"/> </xsl:attribute> <Fornamn> <xsl:value-of select="@Fornamn"/> </Fornamn> <Efternamn> <xsl:value-of select="@Efternamn"/> </Efternamn> </person> </xsl:for-each> </begaranominkomstuppgift> </xsl:template> </xsl:stylesheet> 

into this xml

 <begaranominkomstuppgift>    <person personnummer="8802202220">      <fornamn>Arne</fornamn>      <efternamn>Jonsson</efternamn>    </person>    <person personnummer="8702102110">      <fornamn>Gunilla</fornamn>      <efternamn>Jonsson</efternamn>    </person> </begaranominkomstuppgift> 

But all I get is a blank page

Change your XSL to this:

<?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"/>

    <xsl:template match="/">
      <begaranominkomstuppgift>
        <xsl:for-each select="ForskoleAnsokan/Vardnadshavare/Vardtagare">
          <person>
            <xsl:attribute name="Personnummer">
              <xsl:value-of select="Personnummer"/>
            </xsl:attribute>
            <Fornamn>
              <xsl:value-of select="Fornamn"/>
            </Fornamn>
            <Efternamn>
            <xsl:value-of select="Efternamn"/>
            </Efternamn>
          </person>
        </xsl:for-each>
      </begaranominkomstuppgift>
    </xsl:template>
</xsl:stylesheet>

You were using the attribute accessor (@) in your value-of calls, but those data items are just elements, not attributes.

If you want the text to be displayed, you need to change the output to html. Change your xslt.xsl file to this :

<xsl:template match="/">
  <begaranominkomstuppgift>
    <xsl:for-each select="ForskoleAnsokan/Vardnadshavare/Vardtagare">
      <person>
        <xsl:attribute name="Personnummer">
          <xsl:value-of select="Personnummer"/>
        </xsl:attribute>
        <Fornamn>
          <xsl:value-of select="Fornamn"/>
        </Fornamn>
        <Efternamn>
        <xsl:value-of select="Efternamn"/>
        </Efternamn>
      </person>
    </xsl:for-each>
  </begaranominkomstuppgift>
</xsl:template>

Notice that I removed the @ as they are used to access attribute not simple elements.

Also note that your xml was actually already transforming, but as you stated that it display as a blank page, I supposed you are trying to view in browser and then need to change the output to html. For example, if you inspect the page, you will see that the xml is outputed.

Also note that changing the output to html won't display the actual XML, but will interpret it as html and then output some content. To see your exact xml, simply right click on the page and click inspect.

Also note that this won't work on chrome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM