XSLT 应用模板和字符串操作
我有一些像这样的 XML:
<subsection number="5">
<p>
(5) The <link path="123">Secretary of State</link> shall appoint such as....
</p>
</subsection>
我无法更改 XML,我需要删除段落开头的 (5) 并使用父标记中的 number 属性来创建带有适当标记的新段落编号
<xsl:template match="subsection/p">
<xsl:variable name="number">
<xsl:text>(</xsl:text>
<xsl:value-of select="../@number"/>
<xsl:text>)</xsl:text>
</xsl:variable>
<xsl:variable name="copy">
<xsl:value-of select="."/>
</xsl:variable>
<p>
<span class="indent">
<xsl:value-of select="$number" />
</span>
<span class="copy">
<xsl:value-of select="substring-after($copy, $number)" />
</span>
</p>
</xsl:template>
:问题是该段落的其余部分可能包含更多需要转换的 XML,例如示例中的链接标记。
使用 substring-after 函数后,我不知道如何将模板应用于此。
I have some XML like so:
<subsection number="5">
<p>
(5) The <link path="123">Secretary of State</link> shall appoint such as....
</p>
</subsection>
I can't change the XML and I need to strip out the (5) at the start of the paragraph and use the number attribute in the parent tag to create a new paragraph number with appropriate markup:
<xsl:template match="subsection/p">
<xsl:variable name="number">
<xsl:text>(</xsl:text>
<xsl:value-of select="../@number"/>
<xsl:text>)</xsl:text>
</xsl:variable>
<xsl:variable name="copy">
<xsl:value-of select="."/>
</xsl:variable>
<p>
<span class="indent">
<xsl:value-of select="$number" />
</span>
<span class="copy">
<xsl:value-of select="substring-after($copy, $number)" />
</span>
</p>
</xsl:template>
The problem is the rest of the paragraph may contain more XML that needs to be transformed, such as the link tag in the example.
I don't know how to apply templates to this once I've used the substring-after function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种显式方法是将
subsection/p
元素的第一个文本子元素与所有其他子元素分开处理。出于演示目的,我还添加了一个用于将link
元素转换为a
元素的模板。该样式表产生以下输出:
An explicit approach is to handle the first text child of
subsection/p
elements separately from all other children. For demonstration purposes, I've also added a template for convertinglink
elements intoa
elements.This stylesheet produces the following output: