0

I am trying to convert the following XML to JSON for an assignment but am getting the error:

This XML file does not appear to have any style information associated with it.

source.xml

<people>
  <name>John</name>
</people>

converter.xslt

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
{
    "people":       
    {
        <xsl:for-each select="people">
        "name":
            ["
            <xsl:value-of select="people/name"/>
                "]
        </xsl:for-each>
    }
}
</xsl:template>
</xsl:stylesheet>

Any advice as to what I am doing wrong is appreciated. I believe the issue is the value-of element but cant understand what the issue is.

user3385136
  • 513
  • 1
  • 7
  • 20

2 Answers2

2

Add this first line to your source.xml file so that it looks like this:

<?xml-stylesheet type="text/xsl" href="converter.xslt" ?>
<people>
  <name>John</name>
</people>

and remove the first line from your XSLT and the 'people/' reference before 'name' so that it looks like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
  {
    "people":       
    {
        <xsl:for-each select="people">
        "name":
            ["
            <xsl:value-of select="name"/>   <!-- REMOVE the 'people' before the 'name' -->
                "]
        </xsl:for-each>
    }
  }
  </xsl:template>
</xsl:stylesheet>

This should work. If you access it with your browser with

file:///home/path/source.xml

The result is:

{
    "people":       
    {

        "name":
            ["
            John
                "]

    }
}
zx485
  • 28,498
  • 28
  • 50
  • 59
0

I think, in the final version you will have several names, e.g.:

<people>
    <name>John</name>
    <name>George</name>
    <name>Eva</name>
</people>

The output name list should:

  • have each name in double quotes,
  • names should be separated with a comma,

something like this: [ "John", "George", ... ]

Try the following XSL:

<?xml version="1.0" encoding="UTF-8" ?>  
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <xsl:variable name="names" select="people/name"/>
    <xsl:variable name="separ">", "</xsl:variable>
{
    "people":
    {  
        "name": ["<xsl:value-of select="$names" separator="{$separ}"/>"]
    }
}
  </xsl:template>
</xsl:stylesheet>

Then the output list meets the above requirement.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
  • You can't use `separator` in XSLT 1.0. Also, in 1.0, your variable `$names` is only going to contain the first `name`. – Daniel Haley Jan 08 '17 at 15:51
  • I checked the above solution using [http://xslttest.appspot.com/](http://xslttest.appspot.com/) and got the proper result: `"name": ["John", "George", "Eva"]`. – Valdi_Bo Jan 11 '17 at 17:06
  • That's because that tool "added support for XSLT 2.0" and apparently that support is included even when the stylesheet version is 1.0. Try it with another 1.0 processor (Xalan, Saxon 6.5.5., http://xsltransform.net/ (when the website is actually up), browser (like the OP is doing), etc.) and you'll see that your solution won't work. – Daniel Haley Jan 11 '17 at 17:24