0

次の XML が与えられた場合

<?xml version="1.0" encoding="UTF-8"?>
<root>
<record>
    <title>TITLE</title>
    <key>FIRSTKEY</key>
    <value>FIRSTVALUE</value>
</record>
<record>
    <title>TITLE</title>
    <key>SECONDKEY</key>
    <value>SECONDVALUE</key>
</record>

したがって、各レコードには同じ TITLE があります。

XSLT でやりたいことは、最初の情報 (またはすべて同じヘッダー情報を持つ任意の要素) に基づいてヘッダーを生成することですが、同じドキュメント内で、すべてのノードをループしたいのですが、そのようです:

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="utf-8" indent="yes"/>
    <xsl:template match="/root">
        <doc>
        <!-- xsl:select first node -->
        <header><title><xsl:value-of select="title"/></title></header>
        <!-- /xsl:select -->
        <!-- xsl:for-each loop over all nodes, including the one we selected for the header -->
            <key><xsl:value-of select="key"/></key>
            <value><xsl:value-of select="value"/></value>
        <!-- /xsl:for-each -->
        </doc>
    </xsl:template>
</xsl:transform>

どうすればこれを行うことができますか?

4

1 に答える 1

1

使用する場合

<xsl:template match="/root">
    <!-- xsl:select first node -->
    <header><title><xsl:value-of select="record[1]/title"/></title></header>
    <!-- /xsl:select -->
    <!-- xsl:for-each loop over all nodes, including the one we selected for the header -->
     <xsl:for-each select="record">
        <key><xsl:value-of select="key"/></key>
        <value><xsl:value-of select="value"/></value>
     </xsl:for-each>
    <!-- /xsl:for-each -->
</xsl:template>

必要な結果が得られるはずです。

ただし、2 つのモードでテンプレート ベースのアプローチを使用することをお勧めします。

    <xsl:template match="/root">

        <xsl:apply-templates select="record[1]" mode="head"/>
         <xsl:apply-templates select="record"/>
    </xsl:template>


<xsl:template match="record" mode="head">
  <header><xsl:copy-of select="title"/></header>
</xsl:template>

<xsl:template match="record">
  <xsl:copy-of select="key |  value"/>
</xsl:template>
于 2013-10-15T09:27:53.127 に答える