1

私はこのxmlファイルを持っています:

<?xml version="1.0" encoding="iso-8859-1"?>
<doclist>
<text attribute="a">This is a <tag1>sentence</tag1> <tag1>with</tag1> a few            
<tag1>words</tag1>.</text>
<-- many more text nodes with none, one or several '<tag1>' in it -->
</doclist>

そして、私はこの結果を得たい:

<?xml version="1.0" encoding="iso-8859-1"?>
<doclist>
<text attribute="a">This is a <tag1>sentence with</tag1> a few <tag1>words</tag1>. 
</text>
<-- many more text nodes with none, one or several '<tag1>'s in it -->
</doclist>

私は正規表現でそれをやってみました:

<xsl:template match="text">
<text>
<xsl:apply-templates select="@*"/> <!-- templ. to copy attributes of text -->
<xsl:analyze-string select="." 
regex="&lt;tag1>(.+)&lt;tag1>&lt;tag1>(.+)&lt;/tag1>">
<!-- also tried . instead of &lt; -->
<xsl:matching-substring>
<xsl:for-each select=".">
<tag1>
<xsl:value-of-select="regex-group(1)">
<xsl:text> <xsl:text>
<xsl:value-of-select="regex-group(2)">
</tag1>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:for each select=".">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:non-matching-substring>
</xsl:analyze-string>
</text>
</xsl:template>

しかし、私の出力は次のようになります。

<?xml version="1.0" encoding="iso-8859-1"?>
<doclist>
<text attribute="a>This is a sentencewitha few words. 
</text>
<-- many more text nodes with none, one or several '<tag1>'s in it -->
</doclist>

私の推測では、結果に s が表示されないため、一致するものが見つからないということ<tag1>ですが、タグで囲まれた単語だけが空白を失う理由がわかりません...<tag1>直接の隣人である s を正しく折りたたむにはどうすればよいですか?

4

1 に答える 1

1

for-each-group group-adjacentノード (要素ノードとテキスト ノードの混合コンテンツ) を操作する必要がある場合に使用します。要素ノードの操作には使用できませんanalyze-string

だから私は思う

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

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

<xsl:template match="text">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:for-each-group select="node()" group-adjacent="self::tag1 or self::text()[not(normalize-space())]">
      <xsl:choose>
        <xsl:when test="current-grouping-key()">
          <tag1>
            <xsl:apply-templates select="current-group()"/>
          </tag1>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="current-group()"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

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

</xsl:stylesheet>

そのスタイルシートをSaxon 9に適用すると、入力が変換されます

<doclist>
<text attribute="a">This is a <tag1>sentence</tag1> <tag1>with</tag1> a few            
<tag1>words</tag1>.</text>
<!-- many more text nodes with none, one or several '<tag1>' in it -->
</doclist>

結果に

<doclist>
<text attribute="a">This is a <tag1>sentence with</tag1> a few
<tag1>words</tag1>.</text>
<!-- many more text nodes with none, one or several '<tag1>' in it -->
</doclist>

このアプローチは、より複雑な入力サンプルで機能するはずです。問題がある場合は、より複雑な入力サンプルを質問に追加して、何かテストできるようにしてください。

于 2013-07-03T12:24:57.330 に答える