29

XSLT を使用して文字列を作成し、各文字列をカンマで区切る必要がありますが、最後の文字列の後にカンマを含めないでください。以下の例では、たとえば、Note ノードではなく Distribution ノードがある場合、末尾にコンマが付きます。とにかく、文字列を変数として構築し、XSLT で最後の文字を切り捨てる方法を知りません。また、これは Microsoft XSLT エンジンを使用しています。

私の文字列 =

<xsl:if test="Locality != ''">
  <xsl:value-of select="Locality"/>,
</xsl:if>
<xsl:if test="CollectorAndNumber != ''">
  <xsl:value-of select="CollectorAndNumber"/>,
</xsl:if>
<xsl:if test="Institution != ''">
  <xsl:value-of select="Institution"/>,
</xsl:if>
<xsl:if test="Distribution != ''">
  <xsl:value-of select="Distribution"/>,
</xsl:if>
<xsl:if test="Note != ''">
  <xsl:value-of select="Note"/>
</xsl:if>

[この質問のテキスト ボックスに入力するには、もっと良い方法が必要です :( ]

4

6 に答える 6

53

これは、XSLT を使用すると非常に簡単に実現できます(結果を変数にキャプチャしたり、特別な名前のテンプレートを使用したりする必要はありません)。

I. XSLT 1.0 :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

    <xsl:template match="/*/*">
      <xsl:for-each select=
      "Locality/text() | CollectorAndNumber/text()
     | Institution/text() | Distribution/text()
     | Note/text()
      "
      >
        <xsl:value-of select="."/>
        <xsl:if test="not(position() = last())">,</xsl:if>
      </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

この変換が次の XML ドキュメントに適用された場合:

<root>
    <record>
        <Locality>Locality</Locality>
        <CollectorAndNumber>CollectorAndNumber</CollectorAndNumber>
        <Institution>Institution</Institution>
        <Distribution>Distribution</Distribution>
        <Note></Note>
        <OtherStuff>Unimportant</OtherStuff>
    </record>
</root>

必要な結果が生成されます:

Locality,CollectorAndNumber,Institution,Distribution

必要な要素をドキュメントの順序ではなく生成する必要がある場合 (質問では必須ではありませんが、Tomalak によって提起されたもの)、これを実現するのは非常に簡単でエレガントです。

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>

    <xsl:param name="porderedNames"
     select="' CollectorAndNumber Locality Distribution Institution Note '"/>

    <xsl:template match="/*/*">
        <xsl:for-each select=
         "*[contains($porderedNames, concat(' ',name(), ' '))]">

         <xsl:sort data-type="number"
          select="string-length(
                     substring-before($porderedNames,
                                      concat(' ',name(), ' ')
                                      )
                                )"/>

            <xsl:value-of select="."/>
            <xsl:if test="not(position() = last())">,</xsl:if>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

ここでは、必要な要素の名前とそれらの必要な順序が文字列パラメーター$porderedNamesで提供されます。これには、必要なすべての名前のスペース区切りのリストが含まれます

上記の変換が同じ XML ドキュメントに適用されると、必要な結果が生成されます。

CollectorAndNumber,Locality,Distribution,Institution

Ⅱ.XSLT 2.0 :

XSLT では、このタスクはさらに簡単です (繰り返しますが、特別な機能は必要ありません)。

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

    <xsl:template match="/*/*">
    <xsl:value-of separator="," select=
    "(Locality, CollectorAndNumber,
     Institution, Distribution,
     Note)[text()]" />
    </xsl:template>
</xsl:stylesheet>

この変換を同じ XML ドキュメントに適用すると、同じ正しい結果が生成されます。

Locality,CollectorAndNumber,Institution,Distribution

XPath 2.0 シーケンス タイプ (XSLT 1.0 ソリューションのユニオンとは対照的) を使用しているため、必要な要素は任意の順序で生成されることに注意してください。

于 2009-04-28T16:22:54.440 に答える
7

ノード値を結合するには、短い呼び出しテンプレートを使用したいと思います。これは、連結リストの途中にあるノード (例: Institution) が欠落している場合にも機能します。

<xsl:template name="join">
    <xsl:param name="list" />
    <xsl:param name="separator"/>

    <xsl:for-each select="$list">
        <xsl:value-of select="." />
        <xsl:if test="position() != last()">
            <xsl:value-of select="$separator" />
        </xsl:if>
    </xsl:for-each>
</xsl:template>

これを使用する方法の短い例を次に示します。

サンプル入力ドキュメント:

<?xml version="1.0" encoding="utf-8"?>
<items>
  <item>
    <Locality>locality1</Locality>
    <CollectorAndNumber>collectorAndNumber1</CollectorAndNumber>
    <Distribution>distribution1</Distribution>
    <Note>note1</Note>
  </item>
  <item>
    <Locality>locality2</Locality>
    <CollectorAndNumber>collectorAndNumber2</CollectorAndNumber>
    <Institution>institution2</Institution>
    <Distribution>distribution2</Distribution>
    <Note>note2</Note>
  </item>
  <item>
    <Locality>locality3</Locality>
    <CollectorAndNumber>collectorAndNumber3</CollectorAndNumber>
    <Institution>institution3</Institution>
    <Distribution>distribution3</Distribution>
  </item>
</items>

XSL 変換:

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

  <xsl:template match="/">
    <summary>
      <xsl:apply-templates />
    </summary>
  </xsl:template>

  <xsl:template match="item">
    <item>
      <xsl:call-template name="join">
        <xsl:with-param name="list" select="Locality | CollectorAndNumber | Institution | Distribution | Note" />
        <xsl:with-param name="separator" select="','" />
      </xsl:call-template>
    </item>
  </xsl:template>

  <xsl:template name="join">
    <xsl:param name="list" />
    <xsl:param name="separator"/>

    <xsl:for-each select="$list">
      <xsl:value-of select="." />
      <xsl:if test="position() != last()">
        <xsl:value-of select="$separator" />
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

生成された出力ドキュメント:

<?xml version="1.0" encoding="utf-8"?>
<summary>
  <item>locality1,collectorAndNumber1,distribution1,note1</item>
  <item>locality2,collectorAndNumber2,institution2,distribution2,note2</item>
  <item>locality3,collectorAndNumber3,institution3,distribution3</item>
</summary>

注意: XSLT/XPath 2.0 を使用している場合は、fn:string-joinがあります。

fn:string-join**($operand1 as string*, $operand2 as string*) as string

次のように使用できます。

fn:string-join({Locality, CollectorAndNumber, Distribution, Note}, ",") 
于 2009-04-28T15:34:24.493 に答える
3

次のような入力 XML があるとします。

<root>
  <record>
    <Locality>Locality</Locality>
    <CollectorAndNumber>CollectorAndNumber</CollectorAndNumber>
    <Institution>Institution</Institution>
    <Distribution>Distribution</Distribution>
    <Note>Note</Note>
    <OtherStuff>Unimportant</OtherStuff>
  </record>
</root>

次に、このテンプレートはそれを行います:

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

  <xsl:output method="text" />

  <xsl:template match="record">
    <xsl:variable name="values">
      <xsl:apply-templates mode="concat" select="Locality" />
      <xsl:apply-templates mode="concat" select="CollectorAndNumber" />
      <xsl:apply-templates mode="concat" select="Institution" />
      <xsl:apply-templates mode="concat" select="Distribution" />
      <xsl:apply-templates mode="concat" select="Note" />
    </xsl:variable>

    <xsl:value-of select="substring($values, 1, string-length($values) - 1)" />
    <xsl:value-of select="'&#10;'" /><!-- LF -->
  </xsl:template>

  <xsl:template match="Locality | CollectorAndNumber | Institution | Distribution | Note" mode="concat">
    <xsl:value-of select="." />
    <xsl:text>,</xsl:text>
  </xsl:template>

</xsl:stylesheet>

私のシステムでの出力:

Locality,CollectorAndNumber,Institution,Distribution,Note
于 2009-04-28T15:18:11.023 に答える
2

いくつかのノードをフィルタリングする複雑な選択を使用すると、 position() が正しく機能しないことを言及すると役立つと思います。その場合、次のトリックを思いつきました:

特定の文字で区切られたノードの値を保持する文字列変数を定義し、 str:tokenize() を使用して、位置がうまく機能する完全なノードリストを作成できます。

このようなもの:

<!-- Since position() doesn't work as expected(returning node position of current 
node list), I got round it by a string variable and tokenizing it in which 
absolute position is equal to relative(context) position. -->
<xsl:variable name="measObjLdns" >
    <xsl:for-each select="h:measValue[@measObjLdn=$currentMeasObjLdn]/h:measResults" >
        <xsl:value-of select="concat(.,'---')"/> <!-- is an optional separator. -->
    </xsl:for-each>
</xsl:variable>

<xsl:for-each select="str:tokenize($measObjLdns,'---')" ><!-- Since position() doesn't 

work as expected(returning node position of current node list), 
I got round it by a string variable and tokenizing it in which
absolute position is equal to relative(context) position. -->

    <xsl:value-of select="."></xsl:value-of>
    <xsl:if test="position() != last()">
        <xsl:text>,</xsl:text>
    </xsl:if>
</xsl:for-each>
<xsl:if test="position() != last()">
    <xsl:text>,</xsl:text>
</xsl:if>
于 2012-02-28T14:49:50.393 に答える
1

常にそこにある価値を持っていませんか?もしそうなら、それを好転させて、最初の項目を除いてすべての項目の前にコンマを置くことができます (これは常にそこにあるあなたの値です)。

于 2009-04-28T14:41:11.207 に答える
-4

これは少し面倒ですが、あなたの例のようにいくつかの要素しかない場合はうまくいくかもしれません:

<xsl:if test="Locality != ''">
    <xsl:value-of select="Locality"/>
    <xsl:if test="CollectorAndNumber != '' or Institution != '' or Distribution != '' or Note != ''">
        <xsl:value-of select="','"/>
    </xsl:if>
</xsl:if>
<xsl:if test="CollectorAndNumber != ''">
    <xsl:value-of select="CollectorAndNumber"/>
    <xsl:if test="Institution != '' or Distribution != '' or Note != ''">
        <xsl:value-of select="','"/>
    </xsl:if>
</xsl:if>
<xsl:if test="Institution != ''">
    <xsl:value-of select="Institution"/>
    <xsl:if test="Distribution != '' or Note != ''">
        <xsl:value-of select="','"/>
    </xsl:if>
</xsl:if>
<xsl:if test="Distribution != ''">
    <xsl:value-of select="Distribution"/>
    <xsl:if test="Note != ''">
        <xsl:value-of select="','"/>
    </xsl:if>
</xsl:if>
<xsl:if test="Note != ''">
    <xsl:value-of select="Note"/>
</xsl:if>
于 2009-04-28T15:06:18.523 に答える