2017-01-03 51 views
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="' +&#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>", 

我想要標記進來的引號內,請幫我對此。在此先感謝

回答

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)。

+0

其工作@馬丁。非常感謝。 – User501