XSLT - 通过分析文本节点添加新节点
XSLT - add new node by analyzing text node
我有一个 XML 如下,
<doc>
<chap>
The bowler delivers the ball
to the batsman who attempts to
hit the ball with his bat away from
the fielders so he can run to the
other end of the pitch and score a run.
</chap>
</doc>
我的要求是将名为 <p>
的新节点添加到 <chap>
文本节点,其中将 <p>
节点添加到每一行。
因此,所需的输出是,
<doc>
<chap>
<p>The bowler delivers the ball</p>
<p>to the batsman who attempts to</p>
<p>hit the ball with his bat away from</p>
<p>the fielders so he can run to the</p>
<p>other end of the pitch and score a run.</p>
</chap>
</doc>
你能给我一个建议吗?我如何使用正则表达式在 XSLT 中执行此操作并通过换行符分隔文本 (#xA
)。
我试图完成这个任务,但想不出办法。
您可以使用 xsl:analyze-string
来 select 空格和换行符之间的文本:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="chap/text()">
<xsl:analyze-string select="." regex="\s*(.*)\n">
<xsl:matching-substring>
<p><xsl:sequence select="regex-group(1)"/></p>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>
或者您可以使用 tokenize()
换行拆分
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="chap/text()">
<xsl:for-each select="tokenize(., '\n')[normalize-space()]">
<p><xsl:sequence select="normalize-space()"/></p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我有一个 XML 如下,
<doc>
<chap>
The bowler delivers the ball
to the batsman who attempts to
hit the ball with his bat away from
the fielders so he can run to the
other end of the pitch and score a run.
</chap>
</doc>
我的要求是将名为 <p>
的新节点添加到 <chap>
文本节点,其中将 <p>
节点添加到每一行。
因此,所需的输出是,
<doc>
<chap>
<p>The bowler delivers the ball</p>
<p>to the batsman who attempts to</p>
<p>hit the ball with his bat away from</p>
<p>the fielders so he can run to the</p>
<p>other end of the pitch and score a run.</p>
</chap>
</doc>
你能给我一个建议吗?我如何使用正则表达式在 XSLT 中执行此操作并通过换行符分隔文本 (#xA
)。
我试图完成这个任务,但想不出办法。
您可以使用 xsl:analyze-string
来 select 空格和换行符之间的文本:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="chap/text()">
<xsl:analyze-string select="." regex="\s*(.*)\n">
<xsl:matching-substring>
<p><xsl:sequence select="regex-group(1)"/></p>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>
或者您可以使用 tokenize()
换行拆分
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="chap/text()">
<xsl:for-each select="tokenize(., '\n')[normalize-space()]">
<p><xsl:sequence select="normalize-space()"/></p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>