2016-07-01 3 views
1

私はこのようなXML文書を持っている:現在のXSLTコンテキストから要素名を継承するにはどうすればよいですか?

<?xml version="1.0" encoding="utf-8" ?> 
<article properName="Article"> 
    <h1> 
    This <strong>is a header</strong> 
    </h1> 
    <p>This is a <strong>paragraph</strong</p> 
</article> 

私はこれにそれを変換する必要があります。

<?xml version="1.0" encoding="utf-8" ?> 
<Article> 
    <Article-bigheader> 
    This <Article-bigheader-bold>is a header</Article-bigheader-bold> 
    </Article-bigheader> 
    <Article-paragraph>This is a <Article-paragraph-bold>paragraph</Article-paragraph-bold></Article-paragraph> 
</Article> 

元の文書内の要素は名前を変えていますし、さまざまな方法で入れ子にすることなので、私は可能なすべての組み合わせに対してxslテンプレートを作成する代わりに、これを動的に行う必要があります。私が問題を抱えている部分は、テキストの装飾です。その要素を含むXSLT要素の名前と "-old"の付いた要素を作成するにはどうすればよいですか? これは私がこれまで持っているものです:

<xsl:template match="/article"> 
    <xsl:element name="{@properName}"> 
     <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 
<xsl:template match="h1"> 
    <xsl:element name="{concat(/article/@properName, '-', 'bigheader')}"> 
     <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 
+0

あなたは、XSLT 1.0または2.0を使用していますか? –

答えて

0

おそらく、あなたはこのようにそれを試してみてください:

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="*"/> 

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

<xsl:template match="*[@properName]"> 
    <xsl:element name="{@properName}"> 
     <xsl:apply-templates> 
      <xsl:with-param name="name" select="@properName"/> 
     </xsl:apply-templates> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="h1"> 
    <xsl:param name="name"/> 
    <xsl:element name="{concat($name, '-', 'bigheader')}"> 
     <xsl:apply-templates> 
      <xsl:with-param name="name" select="concat($name, '-', 'bigheader')"/> 
     </xsl:apply-templates> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="p"> 
    <xsl:param name="name"/> 
    <xsl:element name="{concat($name, '-', 'paragraph')}"> 
     <xsl:apply-templates> 
      <xsl:with-param name="name" select="concat($name, '-', 'paragraph')"/> 
     </xsl:apply-templates> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="strong"> 
    <xsl:param name="name"/> 
    <xsl:element name="{concat($name, '-', 'bold')}"> 
     <xsl:apply-templates> 
      <xsl:with-param name="name" select="concat($name, '-', 'bold')"/> 
     </xsl:apply-templates> 
    </xsl:element> 
</xsl:template> 

</xsl:stylesheet> 
+0

これはすばらしい、ありがとう! – FabianGillenius

関連する問題