【发布时间】:2016-12-30 12:04:42
【问题描述】:
我想在引号内添加元素,但如果我使用这个 XSLT 并且我在 XSL 中使用 saxon PE 9.6.0.7 和 version=2.0,它不会出现
我的输入 xml 文件:
<text_top>
<p>Insulin is a medicine that is now an important part of your treatment plan. But you’re probably wondering, why now? Is my diabetes getting worse? Remember, with</p>
</text_top>
<text_bottom>
<p>So, to try and lower your blood glucose levels, your pancreas works overtime to get more insulin into your bloodstream, even though this insulin</p>
<p>As result, blood glucose levels increase, the pancreas eventually wears out, and the cells that produce the insulin in your pancreas are destroyed.</p>
</text_bottom>
XSL 我用作:
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:json="http://json.org/" xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all">
<xsl:template match="text_top">
"top": "<xsl:sequence select="mf:break(normalize-space(string-join(node()/serialize(., $ser-params), '')))"/>",
</xsl:template>
<xsl:template match="text_bottom">
"bottom": "<xsl:sequence select="mf:break(normalize-space(string-join(node()/serialize(., $ser-params), '')))"/>",
</xsl:template>
<xsl:param name="length" as="xs:integer" select="80"/>
<xsl:param name="pattern" as="xs:string" select="concat('((.{1,', $length, '})( |$))')"/>
<xsl:param name="sep" as="xs:string" select="' + '"/>
<xsl:function name="mf:break" as="xs:string">
<xsl:param name="input" as="xs:string"/>
<xsl:variable name="result">
<xsl:analyze-string select="$input" regex="{$pattern}">
<xsl:matching-substring>
<xsl:value-of select="concat('"', regex-group(2), '"')"/>
<xsl:if test="position() ne last()">
<xsl:value-of select="$sep"/>
</xsl:if>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:sequence select="$result"/>
</xsl:function>
</xsl:stylesheet>
我得到的输出 json 为:
"top": "<p>Insulin is a medicine that is now an" +
"important part of your treatment plan. But" +
"you’re probably wondering, why now? Is my" +
"diabetes getting worse? Remember, with</p>",
"bottom": "<p>So, to try and lower your blood glucose" +
"levels, your pancreas works overtime to" +
"get more insulin into your bloodstream, even" +
"though this insulin</p><p>As result, blood glucose levels" +
"increase, the pancreas eventually wears out, and the cells" +
"that produce the insulin in your pancreas are destroyed.</p>"
但我希望 json 输出为:
"top": "<span>Insulin is a medicine that is now an" +
"important part of your treatment plan. But" +
"you’re probably wondering, why now? Is my" +
"diabetes getting worse? Remember, with</span>",
"bottom": "<span>So, to try and lower your blood glucose" +
"levels, your pancreas works overtime to" +
"get more insulin into your bloodstream, even" +
"though this insulin</span>"
"<span>As result, blood glucose levels" +
"increase, the pancreas eventually wears out, and the cells" +
"that produce the insulin in your pancreas are destroyed.</span>"
请在这方面给我建议,在此先感谢。
【问题讨论】:
-
与您的问题没有直接关系,但您想要的输出不是 JSON。 JSON 中没有用于连接字符串的“+”运算符。
-
您已经从其他人那里获得了解决问题的方法。但是请不要在输出 JSON 时不调用它 - 正确准确地使用术语可以让人们更容易相互理解。
-
谢谢@MichaelKay。我一定会追的