1

ある xml を別の xml に変換したい。たとえば、xml タグが文字列の場合、

<book>
<title>test</test>
<isbn>1234567890</isbn>
<author>test</author>
<publisher>xyz publishing</publisher>
</book>

上記のxmlを次のように変換したいのですが、

<b00>
<t001>test</t001>
<a001>1234567890</a001>
<a002>test</a002>
<p001>xyz publishing </p001>
</b00>

PHPを使用してxmlを変換する方法

4

1 に答える 1

3

変換には XSLT を使用できます。

PHPコード

$doc = new DOMDocument();
$doc->load('/path/to/your/stylesheet.xsl');
$xsl = new XSLTProcessor();
$xsl->importStyleSheet($doc);
$doc->load('/path/to/your/file.xml');
echo $xsl->transformToXML($doc);

XSLT ファイル

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" />
    <xsl:template match="/">
        <b00><xsl:apply-templates/></b00>
    </xsl:template>
    <xsl:template match="title">
        <t001><xsl:value-of select="."/></t001>
    </xsl:template>
    <xsl:template match="isbn">
        <a001><xsl:value-of select="."/></a001>
    </xsl:template>
    <xsl:template match="author">
        <a002><xsl:value-of select="."/></a002>
    </xsl:template>
    <xsl:template match="publisher">
        <p001><xsl:value-of select="."/></p001>
    </xsl:template>    
</xsl:stylesheet>
于 2012-10-10T13:09:43.650 に答える