2017-01-27 7 views
0

input.xmlにXSLコピーは、属性

<human gender="male" nationality="american"> 
    <property>blank</property> 
</human> 

(希望)のOutput.xml

<human gender="male" nationality="american"> 
    <property>blank</property> 
</human> 
<person gender="female" nationality="british"> 
    <property>blank</property> 
</person> 

こんにちはみんな、上記の私の希望する変換を交換してください。 私がこれまでに以下のxslを持っている:

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

しかし、どのように私は属性が、私は、XSLを使用してみました 値の交換については行く:しかし、運のない選択するあなたのスタイルシートが近かった

+1

希望出力ドキュメントは整形式XML文書ではありませんそして、私はあなたに、a)一番外側の要素が1つしかない(他の要素はすべて)、またはb)それを '* .xml'という名前にしないでください。また、完全で最小限のXSLTスタイルシートを表示してください。ありがとう。その他のヘルプ:http://stackoverflow.com/help/mcve –

答えて

1

、それだけでテンプレートを欠いを他のテンプレートと一致しないノードと一致するので、built-in templates of XSLTによって取得されません。

属性の変換では、モードを導入することを選択しました。そのため、属性値を変更する2番目のケースでのみ一致するテンプレートがあります。

次のスタイルシート

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

<xsl:template match="human"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    <person> 
     <!-- mode is used to separate this case from the other where things are copied unchanged --> 
     <xsl:apply-templates select="node()|@*" mode="other" /> 
    </person> 
</xsl:template> 

<!-- templates for normal mode --> 

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

<!-- templates for "other" mode --> 

<xsl:template match="@gender" mode="other"> 
    <xsl:attribute name="gender"> 
     <xsl:if test=". = 'male'">female</xsl:if> 
     <xsl:if test=". = 'female'">male</xsl:if> 
    </xsl:attribute> 
</xsl:template> 

<xsl:template match="@nationality" mode="other"> 
    <xsl:attribute name="nationality"> 
     <xsl:if test=". = 'american'">british</xsl:if> 
     <xsl:if test=". = 'british'">american</xsl:if> 
    </xsl:attribute> 
</xsl:template> 

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

</xsl:stylesheet> 

この入力に印加される:

<human gender="male" nationality="american"> 
    <property>blank</property> 
</human> 

は、以下の結果を与える:

<?xml version="1.0" encoding="UTF-8"?> 
<human gender="male" nationality="american"> 
    <property>blank</property> 
</human> 
<person gender="female" nationality="british"> 
    <property>blank</property> 
</person> 
+0

あまりにも早く回答しないでください。質問者が質問を編集して改善する可能性があります。あなたの出産は大したことではありません。性別のひとつは「女性」、「英国人」はそれに含めるべきです。 –

+0

返信用のTks。残念ながら、私はアメリカ人を英国の女性に変換するユースケースを持っています.... – delita

+1

私はそれを反映するために私の答えを更新しました。最初のバージョンは、元のスタイルシートの明白な不完全性を説明しただけです。そのために残念。 – hielsnoppe

関連する問題