1

そこからいくつかのデータを取得して、このxml ファイルの構造を(新しい要素名を持つ) 別のファイルに変換します。

  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>

予想される構造は [更新]:

<?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);

どんな助けでも大歓迎です。

4

1 に答える 1

1

よく提示された質問をありがとう。あなたは正しい軌道に乗っていますが、いくつかの問題があります。

テンプレートを最上位の倍数に適用したいので、最初のテンプレートは次のapply-templatesようになります。

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

はルート要素ではないためMULTIPLE、match 要素の値がスラッシュで始まる場合、2 番目のテンプレートは何にも一致しません。これはあなたが使うべきものです:

<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>

これは単にテンプレートをすべての第 2 レベル<SINGLE>の に適用することに注意してください。章名を取得するために、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>
于 2013-02-07T20:23:20.797 に答える