时间:2016-12-13 12:12:50

标签: json xml xslt

我想在单句(单段)内的行包装末尾添加加号。有可能这样做吗?

我的输入xml文件是:

<response>
    <p>Developing additional problems with your health that are related to your diabetes is by no means a foregone conclusion. There is much you can do to lower your risk.  By accepting your diabetes diagnosis and educating yourself about the complications that can occur and how to prevent them, you have already taken an important first step.</p>
   </response>

我使用XSL作为

 <xsl:stylesheet version="2.0">
       <xsl:template match="response">
               "response": "<xsl:apply-templates/>"
           </xsl:template>
   </xsl:stylesheet>

我的输出需要采用带有+符号的自动换行模式:

   "response": "Developing additional problems with your health that are related to" + 
   "your diabetes is by no means a foregone conclusion. There is much you can do to" +
   "lower your risk.  By accepting your diabetes diagnosis and educating yourself" +
    "about the complications that can occur and how to prevent them, you have" + 
"already taken an important first step."

这可能吗?如果有,请帮助我。提前谢谢

1 个答案:

答案 0 :(得分:1)

下面是一个使用XSLT 2.0的示例和一个使用xsl:analyze-string的函数,该函数使用正则表达式将字符串分解为某个限制的块(定义为整数参数):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mf="http://example.com/mf"
    exclude-result-prefixes="xs mf" version="2.0">

    <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:output method="text"/>

    <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="response">
        "response": <xsl:sequence select="mf:break(normalize-space())"/>
    </xsl:template>

</xsl:stylesheet>

这会将您的样本输入转换为输出

        "response": "Developing additional problems with your health that are related to your" + 
"diabetes is by no means a foregone conclusion. There is much you can do to lower" + 
"your risk. By accepting your diabetes diagnosis and educating yourself about the" + 
"complications that can occur and how to prevent them, you have already taken an" + 
"important first step.

我认为该功能可以缩短为

<xsl:function name="mf:break" as="xs:string">
    <xsl:param name="input" as="xs:string"/>
    <xsl:value-of separator="{$sep}">
        <xsl:analyze-string select="$input" regex="{$pattern}">
            <xsl:matching-substring>
                <xsl:sequence select="concat('&quot;', regex-group(2), '&quot;')"/>
            </xsl:matching-substring>
        </xsl:analyze-string>
    </xsl:value-of>
</xsl:function>
相关问题