我正在使用xsl将xml转换为xml。您能帮我写XSL代码将输入转换为输出吗?
输入:
<ATTRIBUTE-VALUE>
<THE-VALUE>
<div xmlns="http://www.w3.org/1999/xhtml">
<h1 dir="ltr" id="_1536217498885">Main Description</h1>
<p>The main description text goes here.</p>
<h1 dir="ltr" id="_1536217498886">Key Consideration</h1>
<p>The key consideration text goes here.</p>
<h1 dir="ltr" id="_1536217498887">Skills</h1>
<p>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[The main description text goes here.]]></MainDescription>
<KeyConsiderations><![CDATA[The key consideration text goes here.]]></KeyConsiderations>
<Skills>The skills text goes here.</Skills>
<Synonyms>The synonyms text goes here.</Synonyms>
答案 0 :(得分:2)
尝试以下操作(前缀“ x”绑定到xhtml命名空间)
<xsl:template match="THE-VALUE/x:div">
<xsl:for-each-group select="*" group-starting-with="x:h1">
<xsl:element name="{translate(current-group()[1], ' ', '')}">
<xsl:value-of select="current-group()[2]"/>
</xsl:element>
</xsl:for-each-group>
</xsl:template>
实际上,这实际上并不需要分组:您同样可以做<xsl:for-each select="h1">
,然后将组中的两个元素分别称为.
和following-sibling::p[1]
。那就是:
<xsl:template match="THE-VALUE/x:div">
<xsl:for-each select="x:h1">
<xsl:element name="{translate(., ' ', '')}">
<xsl:value-of select="following-sibling::*[1]"/>
</xsl:element>
</xsl:for-eac>
</xsl:template>
后一种解决方案将在XSLT 1.0中使用。