/ / XSLT xsl:copyおよびxsl:for-each on attributes-java、xslt、attributes、foreach、copy

XSLT xsl:属性上のコピーとxsl:for-each - java、xslt、attributes、foreach、copy

次のxslテンプレートがあります。

<xsl:template match="@*|node()" mode="fix-entity-references">
<xsl:copy>
<xsl:for-each select="@*">
<xsl:if test="name() = "href"">
<xsl:variable name="hrefvar">
<xsl:value-of select="current()"/>
</xsl:variable>
<xsl:attribute name="href">
anything
</xsl:attribute>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates select="@*|node()" mode="fix-entity-references"/>
</xsl:copy>
</xsl:template>

このテンプレートを使用して現在のノード。ただし、すべての属性を処理した後にのみ。現在のサンプルは非常に単純で、非常に単純なブロックに置き換えることができます。テスト式はより複雑になります。これが、for-eachが必要な理由です。これらすべての属性を同じように処理したいのですが、常にうまく機能する「current()」値を出力しようとしました。問題は、元のノード「href」属性の値を「anything」に設定しても機能しませんが、それを呼び出すまでには、for-eachブロック内にあるため、現在のノードが属性そのものであることを意味します。

コピーされたノードが変更された属性を使用するように、for-eachブロック内から元のブロックの属性を設定するにはどうすればよいですか?

前もって感謝します。

回答:

回答№1は1

のではなく for-each そして if、別の方法で処理する属性に一致するテンプレートの使用を検討できます。

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

<xsl:template match="@href | @foo | @bar" mode="fix-entity-references">
<xsl:attribute name="{name()}">
<xsl:value-of select=".."/>
</xsl:attribute>
</xsl:template>

XPathデータモデルでは、属性ノードは要素の子ではなく、属性ノードの親と見なされます 属性が属する要素(つまり、コンテキストノードが属性である場合、 .. 属性を見つけることができる要素です)。したがって、このサンプルは任意の値を置き換えます href, foo または bar 属性を含む属性のテキストコンテンツを持つ属性、つまり

<a href="#">http://example.com</a>

になる

<a href="http://example.com">http://example.com</a>