1
我要去从那里取一些数据,即this XML文件的结构转换到另一个(新的元素名称)另一个XML:的转换XML结构使用XSLT和PHP
- 价值所有元素;
- 所有元素的值和元素的元素;
我刚开始玩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>
预计stucture为[更新]:
<?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);
任何帮助,将不胜感激。
非常感谢,这是我真正想要的东西。另外一个问题是为了考虑我将来使用xml处理的过程,我知道有很多方法和解决方案,但是对于像上面这样的情况,转换(解析)的最佳解决方案是什么?顺便说一下,上面提到的response.xml是简化版本,通常它大约是10-30 Mb文件,当然我想使用该解决方案,这是转换或解析此类xml文件中速度最快的解决方案。预先感谢您 – Dozent
忘了提及,您提出的方法是只采用每个** **元素的第一** **单元,如果您查看原始的response.xml也是其他单元素(意味着每章可能有两个或更多的主题)。我应该在上面的元素之前添加另一个for?或者? –
Dozent
就性能而言,我认为上述任何一种解决方案都应该大致相同,因为它们都非常简单。关于在'modules'组下面处理多个'SINGLES',从你的例子中不清楚你想如何在输出XML中表示,那么你能解释一下吗?你能否举一个这样的结果应该看起来如何的例子?我现在要睡觉了,但一旦你澄清了这一点,我可以在大约7小时内更新我的答案。 – JLRishe