[英]Luracast Restler 3 RC6: How to rename XML objects names
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: JSON输出是:
{
"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: XML输出:
<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. 问题是我希望文件在
<file>
标签内,而不是在<item>
标签内。
Here is an example XML output I would like to get: 这是我想要获得的示例XML输出:
<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
我猜答案是针对Restler 1或2,因为它太旧了: Luracast Restler:“Naming”返回对象
EDIT: 编辑:
Changing the XmlFormat::$defaultTagName = 'file';
更改
XmlFormat::$defaultTagName = 'file';
or something like that is not an option, since I need to rename other <item>
tags also in the same request. 或类似的东西不是一个选项,因为我需要在同一个请求中重命名其他
<item>
标签。
EDIT 2: 编辑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? 我知道这可以通过创建我自己的“XmlFormat.php”文件来实现,该文件具有我想要的格式,但是当前的原始文件是否支持这种自定义(根据这个答案: Luracast Restler:“命名“返回的对象”还是稍后删除了此功能?
我认为没有办法做到这一点,没有创建我自己的格式
You can use XSL transformation. 您可以使用XSL转换。 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: 你会得到这样的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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.