2017-06-28 5 views
0

要素を属性に変換しています。属性として変換しようとした要素に存在するかどうかを親要素の名前で確認しています。いくつかの要素を無視してカスタム条件で属性を指定する

planet_nameのような例は要素であり、それは親要素(惑星)を含んでいるので、それをそのまま属性として秘密にする必要があり、parent_elementやplanet_numberのような要素を除外したい。

私はxsltを書いていますが、動作していないのは誰ですか?これについてお手伝いしてください。前もって感謝します。

サンプルXML例:

<world> 
      <planet> 
       <planet_name>solaris</planet_name> 
       <planet_number>23</planet_number> 
       <test>value1</test> 
       <parent> 
        <test>test1</test> 
        <parent_element>test</parent_element> 
       </parent> 
      </planet> 
</world> 

予想される出力:

<world> 
      <planet planet_name="solaris"> 
       <planet_number>23</plante_number> 
       <parent> 
        <test>test1</test> 
        <parent_element>test</parent_element> 
       </parent> 
       <test>value1</test> 
      </planet> 
</world> 

XSLT:

<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="*"> 
    <xsl:copy> 
     <xsl:copy-of select="@*"/> 
     <xsl:apply-templates select="*[starts-with(name(), name(current()))]" mode="attr"/> 
     <xsl:apply-templates select="node()[not(starts-with(name(), name(current())))]"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="*" mode="attr"> 
    <xsl:attribute name="{name()}"> 
     <xsl:value-of select="." /> 
    </xsl:attribute> 
</xsl:template> 

</xsl:stylesheet> 
+1

あなたのXSLTは非常に一般的です(良いですが)、要素を除外するための正確なルールは何ですか?あるいは、除外する必要がある要素の名前をXSLTにハードコードすることを実際にしたいのですか?ありがとう! –

+0

私は要素を除外するための特別な規則はありませんが、除外する要素のリストはありますか?私はリストを持っていて、要素をスキップするためにxsltでハードコードしたいと思っています – Bablu

+0

Martin Honnenはあなたに良い答えをくれましたが、https://stackoverflow.com/a/44554260/3016153も参照してください。 –

答えて

0

<xsl:apply-templates select="*[starts-with(name(), name(current()))]" mode="attr"/> 
を変更します0

<xsl:apply-templates select="node()[not(starts-with(name(), name(current()))) or name() ='planet_number' or name() = 'parent_element']"/> 

<xsl:apply-templates select="*[starts-with(name(), name(current())) and not(name() = 'planet_number' or name() = 'parent_element')]" mode="attr"/> 

およびその他のapply-templatesには行う必要があります。​​。

関連する問題