0

変換する xml データの次の構造があります。

     <chapter>
          <title>Text</title>
          <subtitle>Text</subtitle>
          <paragraph>Text</paragraph>
          <paragraph>Text</paragraph>
          <subtitle>Text</subtitle>
          <paragraph>Text</paragraph>
          <other>Text</other>
      </chapter>

      <chapter>
          <title>Text</title>
          <paragraph>Text</paragraph>
          <paragraph>Text</paragraph>
          <paragraph>Text</paragraph>
          <other>Text</other>
      </chapter>

      <chapter>
          <title>Text</title>
          <paragraph>Text</paragraph>
          <subtitle>Text</subtitle>
          <paragraph>Text</paragraph>
          <paragraph>Text</paragraph>
          <other>Text</other>
      </chapter>

ご覧のとおり、各章の字幕には永続的なパターンはありません。出力では、xml にあるのと同じ場所に字幕を設定する必要があります。段落タグには、for-each ループを使用します。このような:

<xsl:template match="chapter">
  <xsl:for-each select="paragraph">
    <xsl:value-of select="."/>
  </xsl:for-each>
</xsl:template>

そして今、字幕を上、段落の間、または段落の間に、xml にある順序で設定する必要があります。これどうやってするの?助けてください!

4

2 に答える 2

1

を使用して

 <xsl:for-each select="paragraph">

最初にすべての段落要素を引き出します。これを次のように変更できます。

<xsl:for-each select="*">

すべての要素を順番に処理しますが、for-eachを避け、代わりにapply-templatesを使用することをお勧めします(または少なくともより慣用的なxslt)。

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


<xsl:template match="title">
Title: <xsl:value-of select="."/>
</xsl:template>


<xsl:template match="subtitle">
SubTitle: <xsl:value-of select="."/>
</xsl:template>


<xsl:template match="paragraph">
 <xsl:text>&#10;&#10;</xsl:text>
 <xsl:value-of select="."/>
<xsl:text>&#10;&#10;</xsl:text>
</xsl:template>
于 2013-01-22T17:09:40.977 に答える
0

これはそれをしますか?

<xsl:template match="chapter">
  <xsl:for-each select="paragraph | subtitle">
    <xsl:value-of select="."/>
  </xsl:for-each>
</xsl:template>

しかし、David Carlisleが指摘しているように、典型的なXSLTアプローチは、これをテンプレートに分割することです。これは、特定のテンプレートに特別な処理が必要な場合に特に意味があります。

<xsl:template match="chapter">
   <xsl:apply-templates select="paragraph | subtitle" />
</xsl:template>

<xsl:template match="paragraph">
   <xsl:value-of select="." />
</xsl:template>

<xsl:template match="subtitle">
   <!-- do stuff with subtitles -->
</xsl:template>
于 2013-01-22T17:06:58.047 に答える