2016-07-29 11 views
1

以下の入力XMLがあるを追加しますGとBです。私の戦略は、最初のノードの後に​​各色の値ごとに新しい要素を追加することです。XSLT 1.0は、新しい要素

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

<xsl:template match="/ImageProductOrder/color"> 
//insert another color element here 
</xsl:template> 

実際にXSLTを使用してこれを実装する方法がわかりません。または、これを機能させる別の戦略がありますか?

+0

これはXSLT 1.0用です –

+0

"*はR、G、Bの任意の組み合わせです。*" R、G、Bの値はすべて事前に分かっていますか? - "*これはXSLT 1.0用です*"特にXSLT 1.0プロセッサはどれですか? –

+0

はい値はR、G、Bのすべての組み合わせです –

答えて

1

値は、ちょうどすべてのR、Gの組み合わせとBです

さて、あなたは、単に行うことができます:

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="colors"> 
    <xsl:if test="contains(., 'R')"> 
     <colors>R</colors> 
    </xsl:if> 
    <xsl:if test="contains(., 'B')"> 
     <colors>B</colors> 
    </xsl:if> 
    <xsl:if test="contains(., 'G')"> 
     <colors>G</colors> 
    </xsl:if> 
</xsl:template> 

</xsl:stylesheet> 
0

より一般的にアプローチは再帰的なテンプレートを使用して(設定可能な)区切り文字を分割することです:

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

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

    <xsl:template match="colors"> 
     <xsl:call-template name="make-color"> 
      <xsl:with-param name="val" select="."/> 
     </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="make-color"> 
     <xsl:param name="val"/> 
     <xsl:param name="delim" select="'+'"/> 
     <xsl:choose> 
      <xsl:when test="contains($val, $delim)"> 
       <color><xsl:value-of select="substring-before($val, $delim)"/></color> 
       <xsl:call-template name="make-color"> 
        <xsl:with-param name="val" select="substring-after($val, $delim)"/> 
        <xsl:with-param name="delim" select="$delim"/> 
       </xsl:call-template> 
      </xsl:when> 
      <xsl:otherwise> 
       <color><xsl:value-of select="$val"/></color> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 

</xsl:stylesheet> 
関連する問題