2009-06-04 12 views
3

私はXMLを取り入れてきれいにフォーマットされたXHTMLを生成する既存のXSLTスタイルシートを持っています。私はこのスタイルシートのXSL-FO版を作成して、Apache FOPを介してPDFを作成したいと考えています。私が知りたいことはこれです:ノードのほとんどをコピーしますが、追加 XSLTを使用してXSLTを生成するための便利な構成要素は何ですか?

  • 変更されていないいくつかのノードをコピー

    • は、私のようなことを行うことを学ぶ必要があるXSLTパターンを使用して任意の便利なあります余分な属性

    は、私が使用して新しいノードを作成することができます知っている

    <xsl:element> 
    

    しかし、私が必要とする他の有用なものがあります。あるXSLT形式から別の形式に多くのコピーを行ったわけではありませんが、XSLTを使用してXML-> XHTMLのTONSを作成しましたので、言語の核心のほとんどに精通しています。

  • 答えて

    8

    パターンは、「修正アイデンティティ変換」です。このアプローチの基礎は、以下のスタイルシートの最初のテンプレートルールであるID変換ルールです。それ以降の各ルールは、コピー動作の例外を表します。

    <xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    
        <!-- By default, copy all nodes unchanged --> 
        <xsl:template match="@* | node()"> 
        <xsl:copy> 
         <xsl:apply-templates select="@* | node()"/> 
        </xsl:copy> 
        </xsl:template> 
    
        <!-- But strip out <foo> elements (including their content) --> 
        <xsl:template match="foo"/> 
    
        <!-- For <bar> elements, strip out start & end tags, but leave content --> 
        <xsl:template match="bar"> 
        <xsl:apply-templates/> 
        </xsl:template> 
    
        <!-- For <bat> elements, insert an attribute and append a child --> 
        <xsl:template match="bat"> 
        <xsl:copy> 
         <xsl:apply-templates select="@*"/> 
         <xsl:attribute name="id">123</xsl:attribute> 
         <xsl:apply-templates/> 
        </xsl:copy> 
        </xsl:template> 
    
    </xsl:stylesheet> 
    

    最後のテンプレートルールで見つかった論理の重複は、私にとって少なくとも満足しています。これは、1つの属性を追加するだけのコードです。そして、もし我々がこれらの束を必要とすると想像してください。ここで私たちは上書きしたいものに、より外科的に正確にすることができます別のアプローチがあります:

    <xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    
        <!-- By default, copy all nodes unchanged --> 
        <xsl:template match="@* | node()"> 
        <xsl:copy> 
         <xsl:apply-templates select="@*"/> 
         <xsl:apply-templates mode="add-atts" select="."/> 
         <xsl:apply-templates/> 
        </xsl:copy> 
        </xsl:template> 
    
          <!-- By default, don't add any attributes --> 
          <xsl:template mode="add-atts" match="*"/> 
    
        <!-- For <bat> elements, insert an "id" attribute --> 
        <xsl:template mode="add-atts" match="bat"> 
        <xsl:attribute name="id">123</xsl:attribute> 
        </xsl:template> 
    
    </xsl:stylesheet> 
    

    は最後に、これはあなたがしたいかもしれない編集の種類ごとに異なるモードを使用して、多くの更なる実施することができる。

    私は時々、これらのカスタムモードをすべて使用

      <!-- By default, don't add anything --> 
          <xsl:template mode="add-atts 
               insert 
               append 
               before 
               after" match="@* | node()"/> 
    

    :定型の一部がわずかに、マルチモードのテンプレートルールのおかげで簡略化することができXSLT 2.0では、

    <xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    
        <!-- For <bat> elements, insert an "id" attribute --> 
        <xsl:template mode="add-atts" match="bat"> 
        <xsl:attribute name="id">123</xsl:attribute> 
        </xsl:template> 
    
        <!-- Append <new-element/> to <bat> --> 
        <xsl:template mode="append" match="bat"> 
        <new-element/> 
        </xsl:template> 
    
        <!-- Insert an element in <foo> content --> 
        <xsl:template mode="insert" match="foo"> 
        <inserted/> 
        </xsl:template> 
    
        <!-- Add content before the <bar/> and <bat/> elements --> 
        <xsl:template mode="before" match="bar | bat"> 
        <before-bat-and-bar/> 
        </xsl:template> 
    
        <!-- Add content only after <bat/> --> 
        <xsl:template mode="after" match="bat"> 
        <after-bat/> 
        </xsl:template> 
    
        <!-- Here's the boilerplate code --> 
        <!-- By default, copy all nodes unchanged --> 
        <xsl:template match="@* | node()"> 
        <xsl:apply-templates mode="before" select="."/> 
        <xsl:copy> 
         <xsl:apply-templates select="@*"/> 
         <xsl:apply-templates mode="add-atts" select="."/> 
         <xsl:apply-templates mode="insert" select="."/> 
         <xsl:apply-templates/> 
         <xsl:apply-templates mode="append" select="."/> 
        </xsl:copy> 
        <xsl:apply-templates mode="after" select="."/> 
        </xsl:template> 
    
          <!-- By default, don't add anything --> 
          <xsl:template mode="add-atts" match="*"/> 
          <xsl:template mode="insert" match="*"/> 
          <xsl:template mode="append" match="*"/> 
          <xsl:template mode="before" match="@* | node()"/> 
          <xsl:template mode="after" match="@* | node()"/> 
    
    </xsl:stylesheet> 
    

    同じスタイルシートでは、しかし、より頻繁に私はそれらをlazily - 必要に応じて追加します。

    +0

    申し訳ありません。これはまさに私が望んでいたものです(私は思います)。私はもう少しそれをテストする必要があります。 –

    3

    XSLTを変換する上で最も大きな障害は、出力名前空間の接頭辞が、トランスフォーム内の実際のXSL命令のプレフィックスと同じであることです。あなたのXSL命令と出力の両方で "xsl:"を使用すると、XSLTエンジンは実行すべきXSL命令と出力すべき命令の違いを認識しないので、XSLTは解析しません。あなたが名前空間のエイリアスを使用しない限り、それは、次のとおりです。

    <xsl:namespace-alias stylesheet-prefix="x" result-prefix="xsl"/> 
    

    <xsl:stylesheet />の内側に配置される。この命令は、あなたがあなたの代わりに名前空間接頭辞を使用して変換であなたの結果のマークアップを書き込むことができます。その後、出力文書が作成されると、実際に必要な接頭辞がエイリアスの場所に挿入されます。だから、例えば、ここにあなたの出力ドキュメントにテンプレートを作成するテンプレートがあります:

    <xsl:template match="xsl:template[@match='title']> 
        <x:template match="title> 
         <x:apply-templates /> 
        </x:template> 
    </xsl:template> 
    

    ここで良い記事です:あなたが探しているhttp://www.xml.com/pub/a/2001/04/04/trxml/

    +0

    さて、xsltには「別のxslt名前空間」があります。 – alamar

    +1

    http://www.w3.org/1999/XSL/Transformは通常の名前空間であり、http://www.w3.org/1999/XSL/TransformAliasは別のものです。 – alamar

    0

    以前はXSL-FOスタイルシートを開発してからRender-X FO2HTML stylesheetを使ってXSL-FOをHTMLに変換しました。それは私が前にそれらを使用していないが、あなたはHTML2FO stylesheetsをしようと考えるかもしれません

    など、 <span><div><inline><block>要素を変換します。または、少なくともいくつかのアイデアを借りるためにそれらを探して。

    FOが提供するページング構文の一部がHTMLに含まれていないため、XSL-FO出力に必要なものすべてが提供されない可能性がありますが、HTMLからXSL-FO要素/属性を文書の本文に追加します。

    関連する問題