4

I have a code such as:

<?php
class Files {
  protected function get() {
    return array(
      'files' => array(
        'file' => array(
          array(
            'filename' => 'test1.jpg',
            'modified' => '2015-01-01 00:00:00',
          ),
          array(
            'filename' => 'test2.jpg',
            'modified' => '2015-01-02 00:00:00',
          ),
          array(
            'filename' => 'test3.jpg',
            'modified' => '2015-01-03 00:00:00',
          ),
        ),
      )
    );
  }
}

The JSON output is:

{
    "files": {
        "file": [
            {
                "filename": "test1.jpg",
                "modified": "2015-01-01 00:00:00"
            },
            {
                "filename": "test2.jpg",
                "modified": "2015-01-02 00:00:00"
            },
            {
                "filename": "test3.jpg",
                "modified": "2015-01-03 00:00:00"
            }
        ]
    }
}

XML output:

<response>
  <files>
    <file>
      <item>
        <filename>test1.jpg</filename>
        <modified>2015-01-01 00:00:00</modified>
      </item>
      <item>
        <filename>test2.jpg</filename>
        <modified>2015-01-02 00:00:00</modified>
      </item>
      <item>
        <filename>test3.jpg</filename>
        <modified>2015-01-03 00:00:00</modified>
      </item>
    </file>
  </files>
</response>

Problem is that I want the files to be within <file> tags, not within <item> tags.

Here is an example XML output I would like to get:

<response>
  <files>
    <file>
      <filename>test1.jpg</filename>
      <modified>2015-01-01 00:00:00</modified>
    </file>
    <file>
      <filename>test2.jpg</filename>
      <modified>2015-01-02 00:00:00</modified>
    </file>
    <file>
      <filename>test3.jpg</filename>
      <modified>2015-01-03 00:00:00</modified>
    </file>
  </files>
</response>

How can I achieve this? I've tried pretty much everything I possibly could come up with, with no luck.

I tried the following answer, but it didn't help. I guess the answer is for Restler 1 or 2, since it's so old: Luracast Restler: "Naming" returned objects

EDIT:

Changing the XmlFormat::$defaultTagName = 'file'; or something like that is not an option, since I need to rename other <item> tags also in the same request.

EDIT 2:

I know this can be achieved by creating my very own "XmlFormat.php" file with a format that I want to have, but does the current original support this kind of customisation like it used to (according to this answer: Luracast Restler: "Naming" returned objects) or is this feature been removed later on?

Community
  • 1
  • 1
Kirbo
  • 381
  • 1
  • 12

2 Answers2

1

I think there is no way to do this, without creating my very own format

Kirbo
  • 381
  • 1
  • 12
0

You can use XSL transformation. Here is code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
    <response>
      <files>
            <xsl:apply-templates select="//file/item"/>
      </files>
    </response>
</xsl:template>
<xsl:template match="//file/*">
    <xsl:for-each select=".">
        <xsl:if test="name()='item'">
            <xsl:element name="file">
                <xsl:copy-of select="./*"/>
            </xsl:element>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

You'll get an XML like this:

<response xmlns:fo="http://www.w3.org/1999/XSL/Format">
<files>
    <file>
        <filename>test1.jpg</filename>
        <modified>2015-01-01 00:00:00</modified>
    </file>
    <file>
        <filename>test2.jpg</filename>
        <modified>2015-01-02 00:00:00</modified>
    </file>
    <file>
        <filename>test3.jpg</filename>
        <modified>2015-01-03 00:00:00</modified>
    </file>
</files>