2017-02-13 9 views
1

特定の属性を削除し、その値を#で囲まれた要素の値として入れようとしています。属性が存在する場合は、それを削除してその値を要素の値として入れてください。

XSLTについての私の知識は残念ながら非常に基本的なものであり、私が使用できるものに類似の質問を翻訳することはできませんでした。私はただの属性を削除します

<xsl:template match="@Attr"> 
</xsl:template> 

の内側に置くものは何でも

。要するに

、XMLのような:

<Parent> 
    <Elem1 Attr="Something" OtherAttr="Other">ExistingValue</Elem1> 
    <Elem2 Attr="SomethingElse" /> 
</Parent> 

はなるはずです:

<Parent> 
    <Elem1 OtherAttr="Other">#Something#</Elem1> 
    <Elem2>#SomethingElse#</Elem2> 
</Parent> 

要素はすでにそれを交換する必要がある値を持っている場合。 Attrという名前の属性以外の属性が存在する場合は、その属性を変更しないでください。属性Attrを持たない要素は変更しないでください。私は、XSLTを使用しますが、このような何かが動作するはずですので、

答えて

2

要素を変更する場合は、属性ではなく要素を操作する必要があります。

はこのようにそれを試してみてください:あなたの入力のための

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="*[@Attr]"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*[not(name()='Attr')]"/> 
     <xsl:value-of select="concat('#', @Attr, '#')"/> 
    </xsl:copy> 
</xsl:template> 


</xsl:stylesheet> 
0

そのはしばらくして:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" version="1.0"> 
    <xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="no" omit-xml-declaration="no"/> 
    <xsl:template match="/"> 
      <xsl:apply-templates select="*"/> 
    </xsl:template> 

<xsl:template match="*"> 
    <xsl:element name="{name()}"> 
     <xsl:apply-templates select="@*"/> 
     <xsl:apply-templates select="*"/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="@*"> 
    <xsl:value-of select="."/> 
</xsl:template> 

</xsl:stylesheet> 
+0

感謝を。残念ながら、特定の名前の属性を持つ特定のノードのみを変更する必要があります。残りはそのまま残すべきです。私はそれを明確にするために質問を編集しました。 – Tony

1

使用このXSLT要素はすでにそれを交換する必要がある値を持つ場合

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" 
    version="2.0"> 
    <xsl:template match="*"> 
     <xsl:copy> 
      <xsl:apply-templates/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="*[@Attr]"> 
     <xsl:copy> 
      <xsl:copy-of select="@* except @Attr"/> 
      <xsl:value-of select="@Attr"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 
+0

この質問は 'xslt-1.0'とタグ付けされています。ソリューションにはXSLT 2.0が必要です。 –

関連する問題