Need to replace the tag along with quotes using XSLT

177 views Asked by At

I want to add element inside the quotes, but its not coming if i used this XSLT and I'm using saxon PE 9.6.0.7 and version=2.0 in XSL

My Input xml file:

<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 I used as:

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

I got the output json as:

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

But I expecting the json output as:

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

Please advice me on this, Thanks in advance.

1

There are 1 answers

2
Martin Honnen On BEST ANSWER

Use the approach from unwanted xml versions coming in Json output using xslt, so change

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

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

to

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

and the necessary variable as shown in the other post.