2016-07-12 1 views
0

スタイリング:XSLT - 私はこのようになり、いくつかの書類を持って変更/可変要素

<p>It is a <noun>truth</noun> <adverb>universally</adverb 
<verb>acknowledged<verb>, that a <adjective>single</adjective> 
<noun>man</noun> in <noun>possession<noun> of a 
<adjective>good</adjective> <noun>fortune</noun>, 
<verb>must</verb> <verb>be</verb> in want of a <noun>wife</noun>.</p> 

(数百ページ長く、より多くの要素名を除いて)私はXSLTを使用したいです特定の要素を含む各段落を表示し、その要素を強調表示する出力HTMLファイルです。したがって、上の段落はすべての出力ファイルに終わるかもしれませんが、一方では動詞が強調表示され、別の動詞では強調表示されます。それはその要素を含んでいないので、 "connectionss"ファイルには全く表示されません。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/">  
     <xsl:result-document method="html" href="nouns.html"> 
      <html> 
       <body> 
        <xsl:for-each select="collection('index.xml')//p[.//noun]"> 
         <p> 
          <xsl:apply-templates select="."/> 
         </p> 
        </xsl:for-each> 
       </body> 
      </html> 
     </xsl:result-document> 
</xsl:template> 

<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="noun"> 
    <span class="highlight"> 
     <xsl:apply-templates select="@*|node()"/> 
    </span> 
</xsl:template> 

(このシナリオではイタリック/上付き文字などのスタイルを他の要素に一致する他のテンプレートがあります)

:私はこのコードを使用してきた名前の要素については

これは完全に機能しますが、非常に長い要素リストのために別々のドキュメントを作成する方法を自動化する必要があります。

<xsl:template match="/"> 
    <xsl:for-each select="speechParts/speechPart"> 
     <xsl:result-document method="html" href="{name}.html"> 
      <html> 
       <body> 
        <xsl:for-each select="collection('index.xml') 
         //p[.//*[name()=current()/elName]]"> 
         <p> 
          <xsl:apply-templates select="."/> 
         </p> 
        </xsl:for-each> 
       </body> 
      </html> 
     </xsl:result-document> 
</xsl:template> 

<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="*[name()=current()/elName]"> 
    <span class="highlight"> 
     <xsl:apply-templates select="@*|node()"/> 
    </span> 
</xsl:template> 

:私は上記のリストに次のスタイルシートを適用することによって、私は必要なすべてのファイルを生成しようとした

<speechParts> 
    <speechPart><name>Nouns</name><tagName>noun</tagName></speechPart> 
    <speechPart><name>Verbs</name><tagName>verb</tagName></speechPart> 
    ... 
</speechParts> 

:私は、これらの要素とそれらの関連を示しています別の文書を持っています

テンプレートでは、 'elName'の現在の値がわからないため、これは機能しません。私は変数を使って年齢を重ねて遊んでいましたが、XSLTでは変数やパラメータをマッチパターンとして使うことはできません。

これは私がそれを作っているように複雑であってはならないようです。これは私が昨日尋ねた質問からの質問にフォローされ(

...そうです、私を助けてどうもありがとうございます - 。それは本当にです私は学ぶことを支援する)

+0

は何かunreasonがありますHTML文書を作成して、のようにHTML文書内で意味をなさない要素をコピーしています。 –

+0

私はそれがちょっと混乱していることは知っていますが、それは私が扱っているXMLです。スパンに変換する必要のあるタグが最初に機能することを確認するのが最も簡単だと分かりました。その後、結果のhtmlのすべてのノイズを後で取り除くことができました。 –

答えて

0

私はあなたがこれらの線に沿って、それを試しをお勧めしたい:。

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/"> 
    <xsl:for-each select="speechParts/speechPart"> 
     <xsl:variable name="elemName" select="tagName"/> 
     <xsl:result-document method="html" href="{name}.html"> 
      <html> 
       <body> 
        <xsl:for-each select="collection('index.xml')//p[.//*[name()=$elemName]]"> 
         <p> 
          <xsl:apply-templates> 
           <xsl:with-param name="elemName" select="$elemName" tunnel="yes"/> 
          </xsl:apply-templates> 
         </p> 
        </xsl:for-each> 
       </body> 
      </html> 
     </xsl:result-document> 
    </xsl:for-each> 
</xsl:template> 

<xsl:template match="*"> 
    <xsl:param name="elemName" tunnel="yes"/> 
    <xsl:choose> 
     <xsl:when test="name()=$elemName"> 
      <span class="highlight"> 
       <xsl:apply-templates/> 
      </span> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:copy> 
       <xsl:apply-templates/> 
      </xsl:copy> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

</xsl:stylesheet> 
+0

これは完全に完璧です。それは私が必要とするものを正確に行います。どうもありがとうございました。 (私のテキストブックにはトンネルの概念は含まれておらず、これは学ぶべき素晴らしいことです。) –

関連する問題