そこからいくつかのデータを取得して、このxml ファイルの構造を(新しい要素名を持つ) 別のファイルに変換します。
- すべての要素の値。
- すべての要素と要素の要素の値。
私は XSLT を使い始めたばかりで、知識が乏しいので、厳密に判断しないでください。私のtransform.xslテンプレートは (xml 要素名を印刷せずに):
<?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="/">
<xsl:apply-templates select="/RESPONSE/MULTIPLE/SINGLE/KEY" />
</xsl:template>
<!-- transformation to another xml file -->
<xsl:template match="/MULTIPLE">
<course>
<xsl:for-each select="SINGLE">
<topic>
<!-- Updated -->
<chapter><xsl:value-of select="KEY[@name='name']/VALUE" /></chapter>
<xsl:for-each select="KEY[@name='modules']/MULTIPLE/SINGLE">
<title><xsl:value-of select="KEY[@name='name']/VALUE" /></title>
<content><xsl:value-of select="KEY[@name='description']/VALUE" /></content>
</xsl:for-each>
<!-- /Updated -->
</topic>
</xsl:for-each>
</course>
</xsl:template>
予想される構造は [更新]:
<?xml version="1.0" encoding="UTF-8"?>
<course>
<topic>
<chapter>Chapter Name 1</chapter>
<title>Title Name 1</title>
<content>Content 1</content>
</topic>
<!-- Updated -->
<topic>
<chapter>Chapter Name 1</chapter> <!-- print for each <title> and <content> -->
<title>Title Name 2</title>
<content>Content 2</content>
</topic>
<topic>
<chapter>Chapter Name n</chapter>
<title>Title Name n</title>
<content>Content n</content>
</topic>
<!-- Updated -->
...
</course>
およびphp手順:
$xml = new DOMDocument;
$xml->load("http://dl.dropbox.com/u/72519118/response.xml");
$xsl = new DOMDocument;
$xsl->load("transform.xsl");
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
echo $proc->transformToXML($xml);
どんな助けでも大歓迎です。