需要帮助操作a中的文本(<;p>;标记,在将xml数据转换为html时不会丢失子节点
我有一个xml文档,它正在使用XSLT2.0转换为HTML文档以供发布 我遇到的问题是,p标记中存在软换行符(LF),需要作为BR标记保留在HTML中,但当我替换BR标记的LF字符时,我会丢失文本中的任何外部参照标记 xml需要帮助操作a中的文本(<;p>;标记,在将xml数据转换为html时不会丢失子节点,html,xml,xslt,xpath,Html,Xml,Xslt,Xpath,我有一个xml文档,它正在使用XSLT2.0转换为HTML文档以供发布 我遇到的问题是,p标记中存在软换行符(LF),需要作为BR标记保留在HTML中,但当我替换BR标记的LF字符时,我会丢失文本中的任何外部参照标记 xml <p>Section 1 <xref href="https://www.google.com/section1">Link</xref> Section 2 <xref href="https://www.google.com/
<p>Section 1
<xref href="https://www.google.com/section1">Link</xref>
Section 2
<xref href="https://www.google.com/section2">Link</xref>
Section 3
<xref href="https://www.google.com/section3">Link</xref>
</p>
第1节
链接
第二节
链接
第三节
链接
在输出中应该是这样的
<p>Section 1 </br><xref href="https://www.google.com/section1">Link</xref><br/>Section 2 </br><xref href="https://www.google.com/section2">Link</xref><br/>Section 3 </br><xref href="https://www.google.com/section3">Link</xref><br/></p>
第1节链接
第2节链接
第3节链接
注意:我没有对上面的内容进行格式化,以说明硬编码LFs转换为BR标记
我们尝试了以下解决方案,但选定的字符串已忽略子节点:
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output encoding="UTF-8"/>
<xsl:template name="replace">
<xsl:param name="string"/>
<xsl:choose>
<xsl:when test="contains($string,' ') ">
<xsl:value-of select="substring-before($string,' ')"/>
<br/>
<xsl:call-template name="replace">
<xsl:with-param name="string" select="substring-after($string,' ')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:if test="xref">
<xsl:copy-of select="xref"/>
</xsl:if>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="p">
<p>
<xsl:call-template name="replace">
<xsl:with-param name="string" select="."/>
</xsl:call-template>
</p>
</xsl:template>
</xsl:stylesheet>
因此,我只需要帮助就可以在不影响子外部参照节点的情况下用BR标记替换LFs。正如您所说的使用XSLT 2一样,使用
xsl:analyze string
似乎可以更轻松地查找字符并用某些元素替换字符
至于您当前方法的问题,只需编写一个
或
(如果您也想在子体中进行替换),并直接在文本节点中进行任何替换,其余部分使用标识转换。这样,您就可以保留文档结构,并且不会删除p
元素的任何子元素
<xsl:template match="p/text()">
<xsl:analyze-string select="." regex=" ">
<xsl:matching-substring>
<br/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
如果您没有像Saxon 9.8或更高版本那样的XSLT 3处理器,请将标识转换解释为
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
并删除XSLT FIDLE示例中使用的
xsl:mode
声明。正如您所说的使用XSLT 2一样,使用xsl:analyze string
似乎会使查找字符和用某些元素替换字符的工作变得更容易
至于您当前方法的问题,只需编写一个
或
(如果您也想在子体中进行替换),并直接在文本节点中进行任何替换,其余部分使用标识转换。这样,您就可以保留文档结构,并且不会删除p
元素的任何子元素
<xsl:template match="p/text()">
<xsl:analyze-string select="." regex=" ">
<xsl:matching-substring>
<br/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
如果您没有像Saxon 9.8或更高版本那样的XSLT 3处理器,请将标识转换解释为
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
并删除XSLT Fiddle示例中使用的
xsl:mode
声明。非常感谢您的快速响应。这正是我想要的。非常感谢您的快速回复。这正是我想要的。