XSLT

2016-11-08 3 views
0

経由でXML属性の変更値Iは、次のXMLファイルがあります:私はそれは次のようになりたいXSLT

<Book description="for beginners" name="IT Book"> 
    <Available>yes</Available> 
    <Info pages="500.</Info> 
</Book> 

を:

<Book description="for pros" name="IT Book"> 
    <Available>yes</Available> 
    <Info pages="500.</Info> 
</Book> 

私は、XML-文書を修正する方法を見上げ適切にインターネット上で。私が最初にすべての私はちょうどすべてをコピーするためのテンプレートを宣言する必要があることが分かった:

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

は、しかし、私は実際の修正のためのテンプレートを作成する方法を知りません。初心者を助けてくれてありがとう。

編集:ここでは、これまで(UL1によって要求されるように)私のスタイルシートです:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sig="http://www.w3.org/2000/09/xmldsig#"> 
    <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/> 

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

    <xsl:template match="@description='for beginners'"> 
     <xsl:attribute name="{name()}"> 
      <xsl:text>for pros</xsl:text> 
     </xsl:attribute> 
    </xsl:template> 
</xsl:stylesheet> 
+1

は、ターゲット属性 '

答えて

1

この質問は、すでに多くの他のスレッドで答えています。例えば。 XSLT: How to change an attribute value during <xsl:copy>?

場合によっては、ID-copyテンプレート以外の属性descriptionに一致するテンプレートが必要です。

<xsl:template match="@description"> <!-- @ matches on attributes, possible to restrict! --> 
    <xsl:attribute name="{name()}"> <!-- creates a new attribute with the same name --> 
    <xsl:text>for pros</xsl:text> <!-- variable statement to get your desired value --> 
    </xsl:attribute> 
</xsl:template> 

EDIT 1(エラーの詳細情報原因)

一つの完全な、有効な、実行可能なスクリプトは次のようになります。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

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

    <xsl:template match="@description[. = 'for beginners']"> 
    <xsl:attribute name="{name()}"> 
     <xsl:text>for pros</xsl:text> 
    </xsl:attribute> 
    </xsl:template> 

</xsl:stylesheet> 
+0

助けてくれてありがとうございます。これは正しい方向性のようです。残念ながら、私のxsl-stylesheetでこれを試してみると、エラーメッセージが表示されます: xsl:attribute:子に要素が既に追加されている場合、要素に属性を追加できません。 – leimooo

+0

コンテキストを追加する前に子ノードを作成するときに、 '子要素が既に要素に追加されている場合、要素に属性を追加できません.'ノードが発生します。注文を変更すると役立つ可能性があります。しかし、いくつかの可能性があります。 ''の前に ''はエラーになります。 – uL1

+0

私のコピーテンプレートで何をしているのですか? : leimooo