gpt4 book ai didi

html - 为什么我的 XSLT 在这里剥离 HTML 标签

转载 作者:数据小太阳 更新时间:2023-10-29 02:10:52 26 4
gpt4 key购买 nike

我正在使用 XSLT 1.0 将一些 XML 转换为 JSON 输出。不幸的是,我正在使用的一些 XML 中包含 HTML 标记。下面是一些 XML 输入的示例:

 <text>
Kevin Love and Steph Curry can talk about their first-
time starting gigs in the All-Star game Friday night when the Minnesota
Timberwolves visit Oracle Arena to face the Golden State Warriors.
</text>
<continue>
<P>
Love and Curry were two of four first-time All-Star starters when the league
made the announcement on Thursday.
</P>
<P>
Love got a late push to overtake Houston Rockets center Dwight Howard in the
final week of voting.
</P>
<P>
"I think it's a little sweeter this way because I really didn't expect it,"
Love said on a conference call. "I was already humbled by the response the
fans gave me to being very close to the top (frontcourt players). The outreach
by the Minnesota fans and beyond was truly amazing."
</P>
</continue>

标记不理想,我需要保留 <P>我的 JSON 输出中的标签。为了处理引号,我将它们转义。这是我处理此问题的模板:

<xsl:variable name="escaped-continue">
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="continue"/>
<xsl:with-param name="replace" select="'&quot;'" />
<xsl:with-param name="with" select="'\&quot;'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="escaped-text">
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="text"/>
<xsl:with-param name="replace" select="'&quot;'" />
<xsl:with-param name="with" select="'\&quot;'"/>
</xsl:call-template>
</xsl:variable>
<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="with"/>
<xsl:choose>
<xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)"/>
<xsl:value-of select="$with"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text"
select="substring-after($text,$replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

然后我简单地使用类似下面的东西来输出 JSON:

{
"text": "<xsl:value-of select="normalize-space($escaped-text)"/>",
"continue": "<xsl:value-of select="normalize-space($escaped-continue)"/>"
}

我这里的问题是输出看起来像这样:

{
"text": "Kevin Love and Steph Curry can talk about their first- time starting gigs in the All-Star game Friday night when the Minnesota Timberwolves visit Oracle Arena to face the Golden State Warriors.",
"continue": "Love and Curry were two of four first-time All-Star starters when the league made the announcement on Thursday. Love got a late push to overtake Houston Rockets center Dwight Howard in the final week of voting. \"I think it's a little sweeter this way because I really didn't expect it,\" Love said on a conference call. \"I was already humbled by the response the fans gave me to being very close to the top (frontcourt players). The outreach by the Minnesota fans and beyond was truly amazing.\"
}

如您所见,双引号已正确转义,但是 <P>标记已被 XSLT 解析器直接剥离和/或解析,然后被 normalize-space() 抑制.重新添加 <P> 的最佳方法是什么?在这里将标签添加到我的输出中?

最佳答案

这样试试:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" />

<xsl:template match="/root">
<xsl:text>{&#10;"text": "</xsl:text>
<xsl:apply-templates select="text/text()"/>
<xsl:text>"&#10;"continue": "</xsl:text>
<xsl:apply-templates select="continue/*"/>
<xsl:text>"&#10;}</xsl:text>
</xsl:template>

<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="text()">
<xsl:variable name="escaped-text">
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="."/>
<xsl:with-param name="replace" select="'&quot;'" />
<xsl:with-param name="with" select="'\&quot;'"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="normalize-space($escaped-text)"/>
</xsl:template>

<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="with"/>
<xsl:choose>
<xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)"/>
<xsl:value-of select="$with"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text"
select="substring-after($text,$replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

应用于输入的修改版本(添加根元素和一些更多标记用于测试):

<root>
<text>
Kevin Love and Steph Curry can talk about their first-
time starting gigs in the All-Star game Friday night when the Minnesota
Timberwolves visit Oracle Arena to face the Golden State Warriors.
</text>
<continue>
<P>
Love and Curry were <i>two of <b>four</b> first-time All-Star</i> starters when the league
made the announcement on Thursday.
</P>
<P>
Love got a late push to overtake Houston Rockets center Dwight Howard in the
final week of voting.
</P>
<P>
"I think it's a little sweeter this way because I really didn't expect it,"
Love said on a conference call. "I was already humbled by the response the
fans gave me to being very close to the top (frontcourt players). The outreach
by the Minnesota fans and beyond was truly amazing."
</P>
</continue>
</root>

产生以下结果:

{
"text": "Kevin Love and Steph Curry can talk about their first- time starting gigs in the All-Star game Friday night when the Minnesota Timberwolves visit Oracle Arena to face the Golden State Warriors."
"continue": "<P>Love and Curry were<i>two of<b>four</b>first-time All-Star</i>starters when the league made the announcement on Thursday.</P><P>Love got a late push to overtake Houston Rockets center Dwight Howard in the final week of voting.</P><P>\"I think it's a little sweeter this way because I really didn't expect it,\" Love said on a conference call. \"I was already humbled by the response the fans gave me to being very close to the top (frontcourt players). The outreach by the Minnesota fans and beyond was truly amazing.\"</P>"
}

关于html - 为什么我的 XSLT 在这里剥离 HTML 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21342565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com