0
我想包括使用XSLT引號裏的標籤,元素標記需要使用XSLT
我輸入的XML尤其occurence包括:
<text>
<top>
<p>Glucose builds up in your blood and your cells become starved for energy and can’t function properly.</p>
</top>
<bottom>
<p>And, for some people, this may be all that is needed to successfully maintain target blood glucose levels.</p>
<p>It doesn’t come in a pill form because it would get destroyed in the stomach during digestion.</p>
</bottom>
</text>
XSL我使用:
<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: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:param name="ser-params" as="element()">
<output:serialization-parameters xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization">
<output:method value="xml"/>
<output:version value="1.0"/>
<output:indent value="yes"/>
<output:omit-xml-declaration value="yes"/>
</output:serialization-parameters>
</xsl:param>
<xsl:template match="top">
"top": <xsl:apply-templates/>,
</xsl:template>
<xsl:template match="bottom">
"bottom": <xsl:apply-templates/>,
</xsl:template>
<xsl:template match="p">
<span><xsl:sequence select="mf:break(normalize-space(string-join(text()/serialize(., $ser-params), '')))"/></span>
</xsl:template>
</xsl:stylesheet>
我得到的輸出:
"top": <span>"Glucose builds up in your blood and your cells become" +
"starved for energy and can’t function properly."</span>,
"bottom": <span>"And, for some people, this may be all that is needed" +
"to successfully maintain target blood glucose levels."</span>
<span>"It doesn’t come in a pill form because it would get destroyed in" +
"the stomach during digestion."</span>,
但我需要輸出爲:
"top": "<span>Glucose builds up in your blood and your cells become" +
"starved for energy and can’t function properly.</span>",
"bottom": "<span>And, for some people, this may be all that is needed" +
"to successfully maintain target blood glucose levels.</span>"
"<span>It doesn’t come in a pill form because it would get destroyed in" +
"the stomach during digestion.</span>",
我想要標記進來的引號內,請幫我對此。在此先感謝
其工作@馬丁。非常感謝。 – User501