パターンは、「修正アイデンティティ変換」です。このアプローチの基礎は、以下のスタイルシートの最初のテンプレートルールである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 - 必要に応じて追加します。
申し訳ありません。これはまさに私が望んでいたものです(私は思います)。私はもう少しそれをテストする必要があります。 –