【问题标题】:Need to add element tags along with '+' symbol in Json Output需要在 Json 输出中添加元素标签和“+”符号
【发布时间】:2016-12-26 11:21:19
【问题描述】:

我想在 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>",

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

【问题讨论】:

  • 您使用哪种 XSLT 处理器?如果您有 Saxon 9 PE 或 EE(例如,在 oXygen 内部),那么您可以先序列化节点,然后调用我在之前关于节点序列化的响应中发布的函数。如果你有 Saxon 9.7 HE,那么你可以简单地设置version="3.0",你甚至可以在开源版本中序列化"description": &lt;xsl:sequence select="mf:break(normalize-space(serialize(node()))"/&gt;
  • 目前我在氧气中使用 Saxon 9 PE 处理器,我尝试使用 3.0 版的 SAXON 9.7 HE,它的输出没有改变@Martin。

标签: javascript json xml xslt


【解决方案1】:

假设您可以使用 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 以调整发生换行的位置。

【讨论】:

  • 它的工作,但我得到这样的输出 "

    这是其他吸烟者的一些东西" xml 版本并随之而来。请对此提供帮助

  • 我使用了发布的代码,然后 Saxon-HE 9.7.0.14J 使用xsl:output method="text" 输出我在回复中显示的内容,我没有得到任何 XML 声明。当你得到不同的输出时,你需要更详细地解释你做了什么以及你使用哪个版本。
  • 我使用的是 Saxon-PE 9.6.0.7,我使用的版本是 3.0 和 xsl:output method="text",但是我得到的输出是“”description": " xml version="1.0" encoding="UTF-8"?>

    这里有一些其他吸烟者说的“+”请帮忙

  • 好吧,如果您需要在特定版本中使用 Saxon PE,请开始阅读其文档saxonica.com/html/documentation9.6/about/index.html 以尝试找出您需要更改的内容。 saxonica.com/html/documentation9.6/functions/fn/serialize.html 似乎建议您可以提供一些带有输出选项的第二个参数。
  • 我试过@Martin,但它没有出现,我将此问题作为新问题提出。请在里面回答。谢谢
猜你喜欢
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 2013-12-04
相关资源
最近更新 更多