0

同じような名前のノードが多数ある XML ファイルを取得しましたが、特定のノード内の属性は一意です。特定の属性値に該当するノードのみをHTMLページに出力したい。

xml は次のとおりです。

<document>
  <component>
    <section>
      <templateId value="temp_1" />
      <entry>
        <act>
          <code displayName="temp_1:code_1" />
        </act>
      </entry>
      <entry>
        <act>
          <code displayName="temp_1:code_2" />
        </act>
      </entry>
      <entry>
        <act>
          <code displayName="temp_1:code_3" />
        </act>
      </entry>
    </section>
    <section>
      <templateId value="temp_2" />
      <entry>
        <act>
          <code displayName="temp_2:code_1" />
        </act>
      </entry>
      <entry>
        <act>
          <code displayName="temp_2:code_2" />
        </act>
      </entry>
    </section>
  </component>
</document>

この特定の例から、temp_2 の templateId 値を持つセクションからのみ displayName 値を取得したいと考えています。これは私が使用している XSL コードですが、必要なセクションだけでなく、すべてを取得しています。正しいヘッダー (span タグの間) が適切に表示されているため、最初の「いつ」が機能していることがわかります。これは、エントリを介した for-each です。

<xsl:tempalte match="/">
<xsl:choose>
  <xsl:when test="//templateId/@value='temp_2'">
    <div style="margin-bottom: 5px; padding: 5px; border-bottom: 1px solid #000000;">
      <span style="font-weight: bold;">Template 2: </span>
      <br />
      <xsl:choose>
        <xsl:when test="count(//section/entry) != 0">
          <xsl:for-each select="//section/entry">
            <xsl:choose>
              <xsl:when test="position() = 1">
                <xsl:value-of select="act/code/@displayName" />
              </xsl:when>
              <xsl:otherwise>
                <br/>
                <xsl:value-of select="act/code/@displayName" />
              </xsl:otherwise>
            </xsl:choose>
          </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
          No codes to display
        </xsl:otherwise>
      </xsl:choose>
    </div>
  </xsl:when>
</xsl:choose>
</xsl:template>

次のように表示されます。

temp_2:code_1
<br>temp_2:code_2

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

4

2 に答える 2

1

XSLT とその哲学を完全に再検討したいのでしょう。BASIC のようにプログラムしないでください。少なくともあなたの場合の基本的なパターンは、XSLT プログラムが一致する要素を処理するためのテンプレートのコレクションであるということです。ifとでコードを散らかす代わりにchoose、適切な一致条件でテンプレートを記述します。BASIC の の代わりに、子を「反復」するためFOR I=1 TO 10に使用します。<xsl:apply-templates/>基本的な考え方は次のとおりです。

<xsl:template match="/">
    <html>
        <xsl:apply-templates/>
    </html>
</xsl:template>

<xsl:template match="templateId"/> <!-- skip templateID elements by default -->

<xsl:template match="templateId[@value='temp_2']">
    <div style="margin-bottom: 5px; padding: 5px; border-bottom: 1px solid #000000;">          
        <span style="font-weight: bold;">Template 2: </span>
        <xsl:apply-templates/>
    </div>
</xsl:template>

<xsl:template match="code">
    <xsl:value-of select="@displayName"/>
    <xsl:if test="position() != 1"><br/></xsl:if>
</xsl:template>

<xsl:template match="section[count(entry)=0]">
    No codes to display
</xsl:template>

act要素のテンプレートがないのはなぜですか? デフォルトでは、XSLT は を行うテンプレートを提供します<xsl:apply-templates/>

于 2013-08-20T17:51:06.207 に答える
0

あなたの説明に基づいて、 for-each に temp_2 値のみが必要なようです。

その場合、選択を次のように更新できます。

<xsl:for-each select="//section[templateId/@value = 'temp_2']/entry">

これは、'temp_2' に等しい属性を持つ のentry下のすべてを取得することを示しています。sectiontemplateIdvalue

于 2013-08-20T17:49:00.200 に答える