【问题标题】:XSLT: wrap text without converting into stringXSLT:包装文本而不转换为字符串
【发布时间】:2013-10-31 13:40:37
【问题描述】:

有没有办法通过将节点的特定部分转换为字符串来包装节点的特定部分而不会丢失子元素?

这就是我所拥有的:

<root>
    <caption>Figure 3.1 Description of an Image, sometimes with <inline>Bold</inline> or <inline>Italic</inline> emphases.</caption>
</root>

...这就是我需要的:

<root>
    <caption><inline>Figure 3.1</inline>Description of an Image, sometimes with <inline>Bold</inline> or <inline>Italic</inline> emphases.</caption>
</root>

我坚持使用正则表达式 ^Figure\s[0-9]+.[0-9]+ 来捕捉不同的变化(例如图 11.10)并尝试了几个小时来解决问题,但如果不删除以下 &lt;inline&gt; 就无法做到……是吗甚至可能?

我正在使用 XSLT 2.0!

谢谢!

【问题讨论】:

    标签: xml xslt xml-parsing xslt-2.0


    【解决方案1】:

    为文本节点编写模板

    <xsl:template match="caption/text()">
      <xsl:analyze-string select="." regex="^Figure\s[0-9]+\.[0-9]+">
        <xsl:matching-substring>
          <inline><xsl:value-of select="."/></inline>
        </xsl:matching-substring>
        <xsl:non-matching-substring>
          <xsl:value-of select="."/>
        </xsl:non-matching-substring>
      </xsl:analyze-string>
    </xsl:template>
    

    当然,您可以使用身份转换模板开始样式表:

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

    【讨论】:

    • 谢谢!工作得很好!我之前尝试过相同的解决方案,但由于“Figure”和 Numbers 之间的 ThinSpace ( ) 而失败了...
    猜你喜欢
    • 2013-08-01
    • 1970-01-01
    • 2014-05-05
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 2019-01-08
    相关资源
    最近更新 更多