我将通过从那里获取一些数据来将此xml 文件的结构转换为另一个(具有新元素名称),即:
- 所有元素的价值;
- all 的值和元素中的元素;
我刚开始玩 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);
任何帮助,将不胜感激。