2013-02-07 106 views
1

我要去从那里取一些数据,即this XML文件的结构转换到另一个(新的元素名称)另一个XML:的转换XML结构使用XSLT和PHP

  1. 价值所有元素;
  2. 所有元素的值和元素的元素;

我刚开始玩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); 

任何帮助,将不胜感激。

回答

1

谢谢你提出的问题。你在正确的轨道上,但也有一些问题:

既然你想模板应用到顶级的倍数,你的第一个apply-templates应该是这样的:

<xsl:apply-templates select="/RESPONSE/MULTIPLE" /> 

由于MULTIPLE是不是根元素,如果匹配元素值以斜杠开始,则第二个模板将不匹配任何内容。这是你应该使用什么:

<xsl:template match="MULTIPLE"> 

而且当你要对一个字符串值进行比较的东西(属性,元素等),你需要把引号围绕价值:

<xsl:value-of select="KEY[@name = 'name']/VALUE" /> 

一旦这些元素是固定的,你得到这个XSLT:

<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" /> 
    </xsl:template> 

    <!-- transformation to another xml file --> 
    <xsl:template match="MULTIPLE"> 
    <course> 
     <xsl:for-each select="SINGLE"> 
     <topic> 
      <chapter> 
      <xsl:value-of select="KEY[@name = 'name']/VALUE" /> 
      </chapter> 
      <title> 
      <xsl:value-of 
       select="KEY[@name= 'modules']/MULTIPLE/SINGLE/KEY 
             [@name = 'name']/VALUE" /> 
      </title> 
      <content> 
      <xsl:value-of 
       select="KEY[@name= 'modules']/MULTIPLE/SINGLE/KEY 
           [@name ='description']/VALUE" /> 
      </content> 
     </topic> 
     </xsl:for-each> 
    </course> 
    </xsl:template> 
</xsl:stylesheet> 

这里是一个更新的版本,以满足您的要求澄清:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" /> 
    <xsl:template match="/"> 
    <course> 
     <xsl:apply-templates 
     select="/RESPONSE/MULTIPLE/SINGLE/KEY[@name = 'modules']/MULTIPLE/SINGLE" /> 
    </course> 
    </xsl:template> 

    <xsl:template match="SINGLE"> 
    <topic> 
     <chapter> 
     <xsl:value-of select="../../../KEY[@name = 'name']/VALUE" /> 
     </chapter> 
     <title> 
     <xsl:value-of select="KEY[@name = 'name']/VALUE" /> 
     </title> 
     <content> 
     <xsl:value-of select="KEY[@name ='description']/VALUE" /> 
     </content> 
    </topic> 
    </xsl:template> 
</xsl:stylesheet> 

请注意,这只是将模板应用于所有二级<SINGLE> s。为了获取章节名称,它将备份XML树以从父节点获取该值。

当您的源XML运行输出是:

<course> 
    <topic> 
    <chapter>General</chapter> 
    <title>News forum</title> 
    <content></content> 
    </topic> 
    <topic> 
    <chapter>ANATOMIE</chapter> 
    <title>1.1 Die Haut</title> 
    <content> 
     &lt;div class="no-overflow"&gt;&lt;p&gt;&lt;span class="nolink"&gt;&lt;img src="http://localhost/pluginfile.php/22/mod_page/intro/die_haut.png" width="auto" style="border: 1px  [SNIP] 
    </content> 
    </topic> 
    <topic> 
    <chapter>ANATOMIE</chapter> 
    <title>1.2 Der Schädel Page</title> 
    <content> 
     &lt;div class="no-overflow"&gt;&lt;h3&gt;1.2 Der Schädel&lt;/h3&gt; 
     [SNIP] 
    </content> 
    </topic> 
    <topic> 
    <chapter>ANATOMIE</chapter> 
    <title>1.6 Die Regelkreise</title> 
    <content> 
     &lt;div class="no-overflow"&gt;&lt;h3&gt;1.6 Die Regelkreise&lt;/h3&gt; 
     [SNIP] 
    </content> 
    </topic> 
    <topic> 
    <chapter>ANATOMIE</chapter> 
    <title>Media</title> 
    <content></content> 
    </topic> 
    <topic> 
    <chapter>NOTFÄLLE</chapter> 
    <title>2.1 Neurologische Notfälle</title> 
    <content></content> 
    </topic> 
    <topic> 
    <chapter>NOTFÄLLE</chapter> 
    <title>2.5 Krampfanfälle</title> 
    <content></content> 
    </topic> 
    <topic> 
    <chapter>NOTFÄLLE</chapter> 
    <title>2.9 Pulmonale Notfälle</title> 
    <content></content> 
    </topic> 
    <topic> 
    <chapter>STÖRUNGEN</chapter> 
    <title>3.1 Störungen der Lebensfunktionen bei Erwachsenen (ab der Pubertät)</title> 
    <content></content> 
    </topic> 
    <topic> 
    <chapter>STÖRUNGEN</chapter> 
    <title>3.16 Störungen der Lebensfunktionen bei Säuglingen (bis ein Jahr) und Kindern (bis zur Pubertät)</title> 
    <content></content> 
    </topic> 
    <topic> 
    <chapter>STÖRUNGEN</chapter> 
    <title>3.25 Starke Blutung</title> 
    <content></content> 
    </topic> 
</course> 
+0

非常感谢,这是我真正想要的东西。另外一个问题是为了考虑我将来使用xml处理的过程,我知道有很多方法和解决方案,但是对于像上面这样的情况,转换(解析)的最佳解决方案是什么?顺便说一下,上面提到的response.xml是简化版本,通常它大约是10-30 Mb文件,当然我想使用该解决方案,这是转换或解析此类xml文件中速度最快的解决方案。预先感谢您 – Dozent

+0

忘了提及,您提出的方法是只采用每个** **元素的第一** **单元,如果您查看原始的response.xml也是其他单元素(意味着每章可能有两个或更多的主题)。我应该在上面的元素之前添加另一个for?或者? – Dozent

+0

就性能而言,我认为上述任何一种解决方案都应该大致相同,因为它们都非常简单。关于在'modules'组下面处理多个'SINGLES',从你的例子中不清楚你想如何在输出XML中表示,那么你能解释一下吗?你能否举一个这样的结果应该看起来如何的例子?我现在要睡觉了,但一旦你澄清了这一点,我可以在大约7小时内更新我的答案。 – JLRishe