2017-03-28 17 views
3

最初のXSLTを書き込もうとしています。属性refが "$ .root"で始まり、 ".newRoot"を挿入するすべてのbind要素を見つける必要があります。特定の属性に一致するように管理していますが、更新された属性値をどのように出力するのかわかりません。特定の属性の値をXSLTで編集する

入力例のXML:これまで

<?xml version="1.0" encoding="utf-8" ?> 
<top> 
    <products> 
     <product> 
      <bind ref="$.root.other0"/> 
     </product> 
     <product> 
      <bind ref="$.other1"/> 
     </product> 
     <product> 
      <bind ref="$.other2"/> 
     </product> 
     <product> 
      <bind ref="$.root.other3"/> 
     </product> 
    </products> 
</top> 

私のXSL:

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

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

    <xsl:template match="bind[starts-with(@ref,'$.root')]/@ref"> 
     <xsl:attribute name="ref">$.newRoot<xsl:value-of select="bind/@ref" /></xsl:attribute> 
    </xsl:template> 
</xsl:stylesheet> 

私は入力から生成したいXML:

<?xml version="1.0" encoding="utf-8" ?> 
<top> 
    <products> 
     <product> 
      <bind ref="$.newRoot.root.other0"/> 
     </product> 
     <product> 
      <bind ref="$.other1"/> 
     </product> 
     <product> 
      <bind ref="$.other2"/> 
     </product> 
     <product> 
      <bind ref="$.newRoot.root.other3"/> 
     </product> 
    </products> 
</top> 

答えて

6

の代わりに:

<xsl:template match="bind[starts-with(@ref,'$.root')]/@ref"> 
    <xsl:attribute name="ref">$.newRoot<xsl:value-of select="bind/@ref" /></xsl:attribute> 
</xsl:template> 

試してみてください。(より便利な構文でも同じこと)

<xsl:template match="bind[starts-with(@ref,'$.root')]/@ref"> 
    <xsl:attribute name="ref">$.newRoot.root<xsl:value-of select="substring-after(., '$.root')" /></xsl:attribute> 
</xsl:template> 

か:

<xsl:template match="bind/@ref[starts-with(., '$.root')]"> 
    <xsl:attribute name="ref"> 
     <xsl:text>$.newRoot.root</xsl:text> 
     <xsl:value-of select="substring-after(., '$.root')" /> 
    </xsl:attribute> 
</xsl:template> 

は、現在のノードを参照する.の使用に注意してください。ご使用のバージョンでは、<xsl:value-of select="bind/@ref" />命令は何も選択しません。ref属性はすでに現在のノードであり、子はありません。

+0

ありがとうございます! ref属性が現在のノードの場合、なぜxsl:attribute要素に名前を付ける必要がありますか? –

+1

@Björn好きな場合は、現在のノードの名前を文字どおりに指定する代わりに計算することができます。 ''。 –

関連する問題