2017-01-03 7 views
0

を使用して、特定の出現に含める必要が要素タグは、私はXSLTを使用して、引用符内のタグを含めるXSLT

私の入力XMLは次のとおりです。

<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> 
:私はとして使用

<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

私はのような出力を得た:

"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

はまあ、あなたはすでにそのトピックに関する様々なバリエーションを掲載している、あなたは答えをたくさん受けている、あなたはすべてのそれらのいずれかを理解したり、なぜあなたはこれまでに、さらに掲載ソリューションを適応することができないでくださいニーズ?

あなたはシリアル化されたマークアップを持ちたいなら、あなたはすでにそれを作成するために、シリアル化機能を使用する方法が示されており、あなたが最初span要素にpの要素を変換したいならば、あなたは、単に基本的なステップであることを行う必要がありますXSLT 2.0インチだから、

<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:template match="p"> 
    <span><xsl:sequence select="mf:break(normalize-space(string-join(text()/serialize(., $ser-params), '')))"/></span> 
    </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などの解析ではないでしょう)。 @Martin作業

+0

その。どうもありがとうございます。 – User501

関連する問題