将文本字符串转换为XML标记,使用XSL pt.2

huangapple go评论69阅读模式
英文:

Convert text string to xml tagging using xsl pt.2

问题

如果我有以下内容:

<p>This is a @e function that does stuff.</p>
<p>This is also a @e function</p>

并且我想使用XSL将'function'包围在<apiname>标签中,示例:

<p>This is a <apiname>function</apiname> that does stuff.</p>
<p>This is also a <apiname>function()</apiname></p>

是否有一种方法可以实现这个?

请注意,字符串'function'会变化,并且<p>标签可以包含多个<apiname>标签。

我尝试了以下方式:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    
    <xsl:template match="*[contains(text(),'@c ')]">
         <xsl:value-of select="substring-before(text(),'@c ')" />
            <apiname>
                <xsl:value-of select="substring-before(substring-after (., '@c '), ' ')" />
            </apiname>
        <xsl:value-of select="substring-after(., substring-before(substring-after (., '@c '), ' '))" />
    </xsl:template>
</xsl:stylesheet>

但它并不总是起作用(我不确定是什么决定了它是否起作用)。它绝对不能在'@e function'组成<p>标签内的最后一个字符时起作用(例如<p>This is also a @e function</p>)。

我正在使用DITA Open Toolkit来编写Markdown,然后在幕后将其转换为XML。然后,XSL应用于XML以显示HTML和其他格式。这就是为什么我不能只从源文件更新XML的原因。

英文:

If I have:

<p>This is a @e function that does stuff.</p>
<p>This is also a @e function</p>

and I want to use XSL to surround 'function' with <apiname> tags, example:

<p>This is a <apiname>function</apiname> that does stuff.</p>
<p>This is also a <apiname>function()</apiname></p>

is there a way to do this?

Note that the string 'function' changes and <p> tags can contain more than one <apiname> tag.

I tried:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    
    <xsl:template match="*[contains(text(),'@c ')]">
         <xsl:value-of select="substring-before(text(),'@c ')" />
            <apiname>
                <xsl:value-of select="substring-before(substring-after (., '@c '), ' ')" />
            </apiname>
        <xsl:value-of select="substring-after(., substring-before(substring-after (., '@c '), ' '))" />
    </xsl:template>
</xsl:stylesheet>

but it doesn't work all the time (I'm not sure what determines whether it does or doesn't). It definitely doesn't when '@e function' makes up the last character within the <p> tagging (ex. <p>This is also a @e function</p>)

I'm using DITA Open Toolkit to write Markdown, which is then converted to XML behind the scenes. Then, XSL is applied to the XML to display HTML and other formats. This is why I can't just update the XML from the source.

答案1

得分: 1

使用xsl:analyze-string

类似以下内容:

<xsl:template match="p">
  <xsl:analyze-string regex="@e\s+([A-Za-z]+)">
    <xsl:matching-substring>
       <apiname>
         <xsl:value-of select="regex-group(1)"/>
       </apiname>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
       <xsl:value-of select="."/>
    </xsl:non-matching-substring>
  </xsl:analyze-string>
</xsl:template>
英文:

Use xsl:analyze-string.

Something like

&lt;xsl:template match=&quot;p&quot;&gt;
  &lt;xsl:analyze-string regex=&quot;@e\s+([A-Za-z]+)&quot;&gt;
    &lt;xsl:matching-substring&gt;
       &lt;apiname&gt;
         &lt;xsl:value-of select=&quot;regex-group(1)&quot;/&gt;
       &lt;/apiname&gt;
    &lt;/xsl:matching-substring&gt;
    &lt;xsl:non-matching-substring&gt;
       &lt;xsl:value-of select=&quot;.&quot;/&gt;
    &lt;/xsl:non-matching-substring&gt;
  &lt;/xsl:analyze-string&gt;
&lt;/xsl:template&gt;

huangapple
  • 本文由 发表于 2023年7月7日 01:36:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76631291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定