为什么我的XSLT在这里剥离HTML标记(Why is my XSLT here stripping HTML tags)
我正在使用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>
标记不理想,我需要在我的JSON输出中保留
<P>
标记。 为了处理报价,我逃避了它们。 这是我处理这个的模板:<xsl:variable name="escaped-continue"> <xsl:call-template name="replace-string"> <xsl:with-param name="text" select="continue"/> <xsl:with-param name="replace" select="'"'" /> <xsl:with-param name="with" select="'\"'"/> </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="'"'" /> <xsl:with-param name="with" select="'\"'"/> </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>
标签重新添加到输出中的最佳方法是什么?I am using XSLT 1.0 in order to convert some XML into JSON output. Unfortunately some of the XML I'm working with has HTML markup in it. Here's an example of some XML input:
<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>
The markup is not ideal and I need to retain the
<P>
tags in my JSON output. In order to deal with quotes, I escape them. Here's my template for handling this:<xsl:variable name="escaped-continue"> <xsl:call-template name="replace-string"> <xsl:with-param name="text" select="continue"/> <xsl:with-param name="replace" select="'"'" /> <xsl:with-param name="with" select="'\"'"/> </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="'"'" /> <xsl:with-param name="with" select="'\"'"/> </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>
I then simply use something like the following to output JSON:
{ "text": "<xsl:value-of select="normalize-space($escaped-text)"/>", "continue": "<xsl:value-of select="normalize-space($escaped-continue)"/>" }
The issue I have here is that the output looks like this:
{ "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.\" }
As you can see, double quotes are properly escaped, however the
<P>
tags have been stripped and/or parsed directly by the XSLT parser and then suppressed bynormalize-space()
. What's the best way to re-add the<P>
tags into my output here?
原文:https://stackoverflow.com/questions/21342565
最满意答案
如果用户处于不同的地区,这是相当合理的 - 您应该问问您的客户他们的系统区域设置是什么。 如果您需要机器可读格式,请使用不变文化。 例如:
String^ value = dateTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo::InvariantCulture);
That's fairly reasonable if the user is in a different locale - you should ask your customer what their system locale is. Use the invariant culture if you want a machine-readable format. For example:
String^ value = dateTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo::InvariantCulture);
相关问答
更多-
除非我错过了你真正的想法,否则你想要的是: import Data.Time getCurrentTime 当在GHCi中运行时,您会得到: 2009-09-28 01:18:27.229165 UTC 或者,在当地时间 (如您所示,我刚刚抓到): getZonedTime 要得到: 2009-09-27 20:22:06.715505 CDT Unless I'm missing what your really after, what you want is: import Data.Time ...
-
SQL Server支持多种格式 - 请参阅MSDN联机丛书(CAST)和转换(CONVERT) 。 大多数这些格式取决于您拥有的设置 - 因此,这些设置可能会工作一些时间 - 有时不会。 解决这个问题的方法是使用SQL Server支持的(稍微调整)的ISO-8601日期格式 - 这种格式始终可以使用 - 无论SQL Server语言和日期格式设置如何。 SQL Server支持的ISO-8601格式有两种: YYYYMMDD只是日期(没有时间部分); 在这里注意: 没有破折号! ,这很重要! YYYY- ...
-
你需要截断。 SELECT CONVERT(DATETIME, CONVERT(CHAR(23), '2010-07-28 20:07:25.733000000')); You'll need to truncate. SELECT CONVERT(DATETIME, CONVERT(CHAR(23), '2010-07-28 20:07:25.733000000'));
-
什么会产生这些奇怪的字符在日期时间字符串转换?(What would produce these strange characters in a DateTime to String conversion?)[2021-08-31]
如果用户处于不同的地区,这是相当合理的 - 您应该问问您的客户他们的系统区域设置是什么。 如果您需要机器可读格式,请使用不变文化。 例如: String^ value = dateTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo::InvariantCulture); That's fairly reasonable if the user is in a different locale ... -
解决了这个问题。 我在这行应该是dateEndTime而不是dateStartTime 。 dateStartTime = If(IsTime(dateEndTime), dateEndTime, "00:00") 所以最终的代码就是这样 If IsDate(dateEnd) Then dateEndTime = If(IsTime(dateEndTime), dateEndTime, "00:00") blog.DateEnd = String.Format("{0} {1}:00", ...
-
使用DateTimeOffset DateTimeOffset dto = DateTimeOffset.ParseExact("20110316 11:03:22.276919 -0400s", @"yyyyMMdd HH\:mm\:ss\.FFFFFF zzz\s", null); Console.WriteLine(dto.DateTime); Use DateTimeOffset DateTimeOffset dto = DateTimeOffset.ParseExact("20110316 11 ...
-
你曾经使用过12小时的hh 。 你想要使用24小时制的HH 。 有关自定义格式字符串的详细信息,请参阅MSDN 。 请注意,您可能希望指定不变文化,除非您确实希望时间分隔符依赖于当前文化: string formatted = StartDate.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture); (请注意,如果您将格式字符串传递给ToString则 ...
-
您似乎在Windows系统上,或者数据源自Windows系统。 在Windows系统上,换行实际上是两个字符: "\r\n" 。 您看到的是^M是该换行符序列的回车符( '\r' )。 删除这些额外字符的一种方法是在循环中使用std::string::find和std::string::erase 。 另一种方法是逐个字符地手动复制到新的std::string ,除非字符是'\r' 。 It seems that you are on a Windows system, or that the data o ...
-
您可以解析通过数组中所有自定义格式的日期: string[] strDates = new string[]{"3/20/2017 4:03:03 PM" ,"03/20/2017 4:03:03 PM" ,"3/20/2017 04:03:03 PM" ,"03/20/2017 04:03:03 PM"}; DateTime dt = DateTime.Now; string[] st ...
-
字符串到日期时间转换 - 指定日期 - 月份日期中的年份(String to Datetime conversion - specifying year in date-month date)[2022-06-21]
1)为什么R假设我想要当年,如果我不想要当年? R假设是因为你有点要求它假设。 问题是:当你为R 1-Mar提供没有一年的1-Mar ,返回的答案可能是系统特定的。 最常见的行为是假设缺少的年,月或日是当前的。 因此,一旦转换完成,您将获得当前年份。 如果碰巧你不想要当年,你可以通过指定年份告诉R。 2)在Python中,使用Pandas - 如何指定我想要的年份以及时区? 同样,当你没有为他们提供有意义的字符串格式时,你真的不能指望Python或者R给你一些有意义的日期。 通过给pandas一个缺少年份的 ...