元素标记需要使用XSLT
问题描述:
我想包括使用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>",
我想要标记进来的引号内,请帮我对此。在此先感谢
答
那么,你已经发布了有关该主题的各种变化,你已经收到了很多答案,你是否了解它们中的任何一个,或者为什么你无法调整到目前为止发布的解决方案需要吗?
如果你想拥有系列化的标记,那么你已经展示了如何使用序列化功能,以创建,如果你首先要转变p
元素span
元素,那么你只需要做到这一点这是一个基本的步骤在XSLT 2.0中。因此,改变
<xsl:template match="p">
<span><xsl:sequence select="mf:break(normalize-space(string-join(text()/serialize(., $ser-params), '')))"/></span>
</xsl:template>
到
<xsl:template match="p">
<xsl:variable name="span">
<span>
<xsl:apply-templates/>
</span>
</xsl:variable>
<xsl:sequence select="mf:break(normalize-space(serialize($span, $ser-params)))"/>
</xsl:template>
,当然使用<xsl:output method="text"/>
作为已经提出了若干倍,输出应该是沿着要求的格式,例如
"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>"
,
(虽然我不明白是什么格式应该代表或实现的,这肯定不是JSON,也不会解析如JavaScript)。
其工作@马丁。非常感谢。 – User501