1

「1-410000 54-420987 63-32000」のような入力パラメーター文字列を xslt 内の構造 (以下のような) に変換して、後で xslt でそのデータを使用する必要があります。

<config:categories>
  <category>
    <value>410000</value>
    <label>1</label>
  </category>
  <category>
    <value>420987</value>
    <label>54</label>
  </category>
  <category>
    <value>32000</value>
    <label>63</label>
  </category>
</config:categories>

PS入力ドキュメントで左部分が見つかった場合、xsltでそのデータを使用して右部分(「-」の後)を抽出するために、「1-410000 54-420987 63-32000」のような文字列を解析する他のオプションはありますか? ?

4

3 に答える 3

2

Dimitreが示すように、XSLT1.0での文字列の解析は非常に面倒です。これは、XSLT2.0がはるかに優れている領域です。これは次のように実行できます。

<categories>
  <xsl:for-each select="tokenize($param, '\s+')">
    <category>
      <label><xsl:value-of select="substring-after(., '-')"/></label>
      <value><xsl:value-of select="substring-before(., '-')"/></value>
    </category>
  </xsl:for-each>
</categories>

そしてもちろん、XSLT 2.0では、node-set()拡張機能を使用せずに、categories構造をファーストクラスのノード値として使用できます。

于 2012-08-09T14:54:47.470 に答える
2

この変換:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:config="some:config" exclude-result-prefixes="config">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pData" select="'1-410000 54-420987 63-32000'"/>

 <xsl:template match="/*">
     <config:categories>
       <xsl:call-template name="gen"/>
     </config:categories>
 </xsl:template>

 <xsl:template name="gen">
  <xsl:param name="pGen" select="$pData"/>

  <xsl:if test="$pGen">
   <xsl:variable name="vChunk" select=
    "substring-before(concat($pGen, ' '), ' ')"/>
      <category>
        <value><xsl:value-of select="substring-after($vChunk,'-')"/></value>
        <label><xsl:value-of select="substring-before($vChunk,'-')"/></label>
      </category>
      <xsl:call-template name="gen">
       <xsl:with-param name="pGen" select="substring-after($pGen, ' ')"/>
      </xsl:call-template>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

任意の XML ドキュメント (使用されていない) に適用すると、必要な正しい結果が生成されます

<config:categories xmlns:config="some:config">
   <category>
      <value>410000</value>
      <label>1</label>
   </category>
   <category>
      <value>420987</value>
      <label>54</label>
   </category>
   <category>
      <value>32000</value>
      <label>63</label>
   </category>
</config:categories>

説明:

substring-before()substring-after()プラス再帰の適切な使用。

于 2012-08-09T12:27:12.250 に答える
0

文字列から XML を作成するには、Java を使用する必要があると思います。それを達成するための XSLT 機能を認識していません。

2 番目の質問: substring()、string-length()、substring-before()、および substring-after() がリクエストに役立つ可能性があります。参照してください: http://www.w3schools.com/xpath/xpath_functions.asp

于 2012-08-09T11:58:10.363 に答える