Luracast Restler 3 RC6: how to rename XML object names

I have a code like:

<?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',
          ),
        ),
      )
    );
  }
}

JSON Output:

{
    "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>

The problem is that I want the files to be in tags <file>, not in tags <item>.

Here is an example of the XML output I would like to receive:

<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 tried almost everything I could come up with, no luck.

I tried the following answer, but that did not help. I assume the answer is for Restler 1 or 2, since it is so old: Luracast Restler: "Naming" returned objects

EDIT:

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

EDIT 2:

, "XmlFormat.php" , , ​​, ( : Luracast Restler: "" ) ?

+4
2

, ,

+1

XSL. :

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

XML:

<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>

0

Source: https://habr.com/ru/post/1619225/


All Articles