2017-03-20 3 views
0

私は変換するXMLを持っている:xslt 2.0による変換の実行引用符付きの値を区切り文字で区切って取得するにはどうすればよいですか?

<root> 
    <item name="first" /> 
    <item name="second" /> 
    <item name="third" /> 
</root> 

私が取得したい何が(順番は関係ありません)すべての値が引用され、値が分離されている文字列です。

"first" , "second" , "third" 

私の質問がありますどうすればxslt 2.0を正しく使用してこれを達成できますか?

ここに私のソリューションは、期待どおりに動作しません。 私の共同質問は、なぜそうではないのですか?

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:ns="urn:my.name.space" 
    version="2.0"> 

    <xsl:output method="text" media-type="text/json"/> 

    <xsl:template match="root"> 
    <xsl:value-of select="item/@name" separator=" , "/> 
    <xsl:text> :: </xsl:text> 
    <xsl:value-of select="item/ns:quotedString(@name)" separator=" , "/> 
    </xsl:template> 

    <xsl:function name="ns:quotedString"> 
    <xsl:param name="input"/> 
    <xsl:variable name="quote">&quot;</xsl:variable> 
    <xsl:value-of select="concat($quote, $input, $quote)"/> 
    </xsl:function> 

</xsl:stylesheet> 

この

は私を与える:私は私の引用機能を呼び出す場合

first , second , third :: "second""third""first" 

注区切りが欠落しています。

私はSaxon-B 9.1.0.8を使用して変換を適用します。

答えて

1

複数の文字列を結合するソリューションは複数あります。以下のための...で...

<xsl:template match="root"> 
    <xsl:value-of select="string-join(for $i in item return concat('&quot;', $i/@name, '&quot;'), ' , ')"/> 
</xsl:template> 

<string-join>

XPathは、各item/@nameのquotatedの文字列を結合します。 <string-join>のパラメータは文字列です。


関数呼び出し "NS:quotedString"

<xsl:template match="root"> 
    <xsl:value-of select="item/ns:quotedString(@name)" separator=" , "/> 
</xsl:template> 

<xsl:function name="ns:quotedString"> 
    <xsl:param name="input"/> 
    <xsl:variable name="quote">&quot;</xsl:variable> 
    <xsl:sequence select="concat($quote, $input, $quote)"/> 
</xsl:function> 

これは、約なぜそれがに動作していない、あなたの質問にお答えします。機能の最後の文として<xsl:sequence>を参照してください。 @separatorが動作するのは、<xsl:value select="...">ステートメントがシーケンスを返す場合、戻り値は単一のテキストノードだけです。

0

この関数は、テキストノードとadjacent text nodes in the sequence are merged into a single text nodeを返します。シーケンスを返しますか?

ので、私の好みはちょうど関数にシーケンスを送信することです

<xsl:function name="ns:quotedString"> 
    <xsl:param name="input"/> 
    <xsl:variable name="quote">&quot;</xsl:variable> 
    <xsl:sequence select="concat($quote, $input, $quote)"/> 
</xsl:function> 

...あなたの関数でxsl:sequenceへのxsl:valueを変更...

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:ns="urn:my.name.space" 
    version="2.0"> 

    <xsl:output method="text" media-type="text/json"/> 

    <xsl:template match="root"> 
    <xsl:value-of select="item/@name" separator=" , "/> 
    <xsl:text> :: </xsl:text> 
    <xsl:value-of select="ns:quotedString(item/@name)"/> 
    </xsl:template> 

    <xsl:function name="ns:quotedString"> 
    <xsl:param name="input"/> 
    <xsl:value-of select="concat('&quot;', 
     string-join($input,'&quot;, &quot;'), 
     '&quot;')"/> 
    </xsl:function> 

</xsl:stylesheet> 

免責事項:私ドン」 Saxon-B 9.1.0.8を持っています。テストする。私はSaxon-HE 9.5.1.7を使用しました。テストする。

+2

"関数が文字列を返します"が間違っていると、テキストノードが返されます。それが文字列を返した場合、式 ''はポスターが望むものを行います。 https://www.w3.org/TR/xslt20/#constructing-simple-content mandates "シーケンス内の隣接するテキストノードは単一のテキストノードにマージされます。" –

+0

@MartinHonnen - 説明をありがとう。私は私の答えを更新します。 –

関連する問題