英文:
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
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论