【问题标题】:Need to replace the tag along with quotes using XSLT需要使用 XSLT 替换标记和引号
【发布时间】: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="' +&#10; '"/>

        <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('&quot;', regex-group(2), '&quot;')"/>
                        <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。我一定会追的

标签: json xml xslt dita


【解决方案1】:

使用unwanted xml versions coming in Json output using xslt的方法,所以改变

 <xsl:template match="text_top">
    "top": <xsl:apply-templates/>,
</xsl:template>

<xsl:template match="text_bottom">
    "bottom": <xsl:apply-templates/>,
</xsl:template>

<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>

以及其他帖子中显示的必要变量。

【讨论】:

  • 它的工作,但我想在输出中将

    标签更改为 标签。它仅在输出中以

    的形式出现。请给建议

  • 我用你的编码编辑过,请看这个并给我建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-21
  • 1970-01-01
相关资源
最近更新 更多