如何使用XSLT </p> </div>在CDATA详细信息部分中添加<div>和<p>标记作为前缀和后缀

时间:2011-08-03 08:00:05

标签: xml xslt

工具: 我正在使用XSLT和XML。

详细说明和问题:

我有一个XML,有一个名为“detail”的节点,下面是XML:

<?xml version="1.0"?>
<news type="detail">
    <article id="694141" published="7/20/2011 1:19:52 PM" language="English">
        <title><![CDATA[myData &amp; Arik Air sign interline agreement]]></title>
        <detail><![CDATA[<strong>Gives passengers greater access to cities across Nigeria and West Africa&nbsp;
            <br /></strong>&nbsp;DUBAI, U.A.E., 13th July 2011: myData (<a target="_new" href="http://www.myData.com/">www.myData.com</a>), one of the world’s fastest growing airlines, has signed an interline agreement with leading Nigerian carrier Arik Air, making it easy for customers to access a wide range of points across Nigeria and West Africa.
            <br />
            &nbsp;
            <br />
            Under the new agreement, customers will be able to purchase joint myData-Arik Air itineraries, enabling them to connect seamlessly from myData’ double daily services to Lagos onto cities including Abuja, Kano, Kaduna, Port Harcort and Enugu .
            <br />
            ]]>
        </detail>
    </article>
</news>

在上面的XML节点“详细信息”中,您可以看到使用了CDATA,现在上面的“详细信息”部分中的内容由编辑进行复制和粘贴,有时他们会将<div><p>放在一起标记在“详细信息”部分的开头,有时他们错过了在复制和粘贴过程中放置​​<div><p>标记,这会使整个格式问题出现在我的页面上,所以现在我想检查是否在该部分的开头有<div><p>,那么它应该正常,否则我们会将<div> <p>添加为前缀</p> {{1作为后缀。为了解决这个问题,我在XSLT中使用了以下逻辑,但是没有成功。

我有一个XSLT,下面是从中获取的一些代码。

</div>

Above Logic工作正常,但是我不想使用“starts-with”而不是这个我想使用像

这样的东西
<xsl:variable name="art_detail" select="/news/article/detail" /> //here I am taking all the node object in variable

<!-- Check if the immediate preceding sibling was a p or div tag? Yes - Use the same XML, No - Add a <div> tag to the xml result --> 
<xsl:variable name="addNode2Detail">
<xsl:choose>
    <xsl:when test="(starts-with($art_detail,'&lt;div&gt;')) or (starts-with($art_detail,'&lt;p&gt;'))">
    <xsl:value-of select="$art_detail"/>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="concat('&lt;div&gt;',$art_detail,'&lt;/div&gt;')"/>
  </xsl:otherwise>
</xsl:choose>
</xsl:variable>

<xsl:variable name="detail" select="utils:DecodeCDATA($addNode2Detail)" />

下面是我尝试编写的示例代码,但没有取得任何成功“msxsl:node-set”再次提出问题并删除所有格式化部分,我的意思是页面看起来很糟糕。

<xsl:when test="($art_detail//preceding-sibling::div or $art_detail//preceding-sibling::p) and node() and string-length(normalize-space(//text())>1)">

请建议任何其他逻辑来实现上述解决方案!!

感谢。

1 个答案:

答案 0 :(得分:0)

您不能使用previous-sibling :: div之类的东西,因为文档中没有div元素。 CDATA的重点是说“这里的东西是字符数据;它可能看起来像标记,但我不希望它被视为标记”。因此,无论谁决定将内容包装在CDATA中,都故意试图阻止你这样做。