元素标记需要特别包含使用XSLT的出现

时间:2017-01-03 06:24:45

标签: xml xslt

我想使用XSLT将标记包含在引号中,

我的输入XML是:

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

我希望标签出现在引号内,请帮我解决这个问题。提前致谢

1 个答案:

答案 0 :(得分:2)

那么,您已经发布了该主题的各种变体,并且您已经收到了很多答案,您是否了解其中任何一个,或者为什么您无法调整目前发布的解决方案以满足其他需求?

如果您想要序列化标记,那么您已经展示了如何使用序列化函数来创建它,如果您首先想要将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)。

相关问题