I want to add the element tags in a json output along with the Plus symbol, at the end of word spaces, I had used some XSLT, in that i got the json output with plus symbol, but the element tag is not coming.
My Input xml is:
<description>
<p>Here are some things other smokers say that they like about smoking:</p>
<ul>
<li>Smoking is a reward I can give myself when I finish a task.</li>
<p>Write your thoughts here. . .</p>
</description>
My XSLT Used:
<xsl:stylesheet version="2.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="xs mf">
<xsl:output indent="yes" omit-xml-declaration="yes" method="xml" encoding="utf-8"/>
<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:template match="description">
"description": <xsl:sequence select="mf:break(normalize-space())"/>,
</xsl:template>
<xsl:template match="p">
<p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="ul">
<ul><xsl:apply-templates/></ul>
</xsl:template>
<xsl:template match="li">
<li><xsl:apply-templates/></li>
</xsl:template>
</xsl:stylesheet>
My Json Output as:
"description": "Here are some things other smokers say that they like about smoking:Smoking is a" +
"reward I can give myself when I finish a task.Write your" +
"thoughts here. . .",
But my expected output as:
"description": "<p>Here are some things other smokers say that they like about smoking:</p><ul><li>Smoking is a" +
"reward I can give myself when I finish a task.</li></ul><p>Write your" +
"thoughts here. . .</p>",
Please help me on this. I want the output with element tags as well as plus symbol. Thanks in advance
Assuming you can use the XPath 3.0/3.1 serialize function ( https://www.w3.org/TR/xpath-functions-31/#func-serialize ), for instance by using Saxon 9.7 HE (or PE or EE of course) and setting version="3.0"
in your stylesheet then you can serialize the nodes you have first and then apply the function to break up the string into chunks. I would also suggest to switch to output method text
as mixing XML or HTML with plain text does not work well with the output method xml
. So try
<xsl:output method="text"/>
<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:template match="description">
"description": <xsl:sequence select="mf:break(normalize-space(serialize(node())))"/>,
</xsl:template>
and the output is like
"description": "<p>Here are some things other smokers say that they like about smoking:</p> <ul>" +
"<li>Smoking is a reward I can give myself when I finish a task.</li> </ul>" +
"<p>Write your thoughts here. . .</p>",
you can change that parameter length
as needed to adjust where the line break happens.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.