我正在使用xsl将xml转换为xml。你能帮我写一段将输入转换成输出的xsl代码吗?我需要的数据作为丰富的文本数据在CDATA的前两个标记。提前谢谢。
注意:我有来自Martin @ XSLT-1.0 How to pick multiple tags between two similar tags as it is?的解决方案,但现在我需要编辑值。如何做到这一点?请帮帮忙。
输入:
<ATTRIBUTE-VALUE>
<THE-VALUE>
<div xmlns="http://www.w3.org/1999/xhtml">
<h1 dir="ltr" id="_1536217498885">Main Description</h1>
<p>Line1 The main description text goes here.</p>
<p>Line2 The main description text goes here.</p>
<p>**<img alt="Embedded Image" class="embeddedImageLink" id="_1536739954166" src="_9c3778a0-d596-4eef-85fa-052a5e1b2166?accept=none&private"/>**</p>
<h1 dir="ltr" id="_1536217498886">Key Consideration</h1>
<p>Line1 The key consideration text goes here.</p>
<p>Line2 The key consideration text goes here.</p>
<h1 dir="ltr" id="_1536217498887">Skills</h1>
<p>Line1 The Skills text goes here.</p>
<p>Line2 The Skills text goes here.</p>
<p>Line3 The Skills text goes here.</p>
<h1 dir="ltr" id="_1536217498888">Synonyms</h1>
<p>The Synonyms text goes here.</p>
</div>
</THE-VALUE>
</ATTRIBUTE-VALUE>输出:
<MainDescription>
<![CDATA[
<p>Line1 The main description text goes here.</p>
<p>Line2 The main description text goes here.</p>
<p>**<img alt="Embedded Image" class="embeddedImageLink" id="_1536739954166" src="_9c3778a0-d596-4eef-85fa-052a5e1b2166.jpg"/>**</p>
]]>
</MainDescription>
<KeyConsiderations>
<![CDATA[
<p>Line1 The key consideration text goes here.</p>
<p>Line2 The key consideration text goes here.</p>
]]>
</KeyConsiderations>
<Skills>
<p>Line1 The Skills text goes here.</p>
<p>Line2 The Skills text goes here.</p>
<p>Line3 The Skills text goes here.</p>
</Skills>
<Synonyms>
<p>The Synonyms text goes here.</p>
</Synonyms>发布于 2018-09-14 16:11:53
如果只想将前两个标记作为CData,只需将此模板添加到您的XSLT中
<xsl:template match="xhtml:h1[position() > 2]">
<xsl:element name="{translate(., ' ', '')}">
<xsl:apply-templates select="key('h1-group', generate-id())"/>
</xsl:element>
</xsl:template> 或者,如果“前两个标签”只指"MainDescription“和"KeyConsideration",请尝试此模板。
<xsl:template match="xhtml:h1[. != 'Main Description' and . != 'Key Consideration']">
<xsl:element name="{translate(., ' ', '')}">
<xsl:apply-templates select="key('h1-group', generate-id())"/>
</xsl:element>
</xsl:template> 编辑:如果您还想“编辑”一个子节点,那么只需为您想要修改的元素或属性添加一个模板。对于每个,要修改img的src属性,请执行以下操作
<xsl:template match="xhtml:p/xhtml:img/@src">
<xsl:attribute name="src">
<xsl:value-of select="concat(substring-before(., '?'), '.jpg')" />
</xsl:attribute>
</xsl:template>注意,这只是一个示例,因为您并没有明确说明修改该值需要什么逻辑,所以我假设您只想用文件扩展名".jpg“替换查询字符串部分。
https://stackoverflow.com/questions/52325687
复制相似问题