繁体   English   中英

需要在Json Output中添加元素标签以及'+'符号

[英]Need to add element tags along with '+' symbol in Json Output

我想将元素标签与加号符号一起添加到json输出中,在单词空间的末尾,我使用了一些XSLT,因为我得到了带有加号符号的json输出,但是元素标签没有到来。

我的输入xml是:

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

我使用的XSLT:

<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="' +&#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: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>

我的Json输出为:

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

但我的预期输出为:

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

请帮我。 我想要带有元素标签以及加号的输出。 提前致谢

假设您可以使用XPath 3.0 / 3.1序列化功能( https://www.w3.org/TR/xpath-functions-31/#func-serialize ),例如通过使用Saxon 9.7 HE(当然也可以是PE或EE) )并在样式表中设置version="3.0" ,则可以先序列化拥有的节点,然后应用该函数将字符串分解为多个块。 我还建议切换到输出方法text因为将XML或HTML与纯文本混合不能很好地与输出方法xml 所以尝试

<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="' +&#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:template match="description">
    "description": <xsl:sequence select="mf:break(normalize-space(serialize(node())))"/>,
</xsl:template>

和输出就像

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

您可以根据需要更改该参数的length ,以调整换行发生的位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM