我对xslt非常陌生。我想将下面的输入转换为输出。我想将h1和h2元素分组,创建属性并在2个h1或h2标签之间添加数据作为子标签值。请帮忙。谢谢。
input.xml
<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>
<h1 dir="ltr" id="_1536217498886">Steps</h1>
<h2 dir="ltr" id="_1536217498886">Section Name One</h2>
<p>Line1 The steps text goes here.</p>
<ul>Line2 The section description text goes here.</ul>
<h2 dir="ltr" id="_1536217498886">Section Name Two</h2>
<p>Line1 The key consideration text goes here.</p>
<ul><li>Line2 The key consideration text goes here.</li></ul>
<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>
output.xml
<MainDescription>
<![CDATA[
<p>Line1 The main description text goes here.</p>
<p>Line2 The main description text goes here.</p>
]]>
</MainDescription>
<Section name="Section Name One">
<Description>
<p>Line1 The section text goes here.</p>
<p>Line2 The description text goes here.</p>
</Description>
</Section>
<Section name="Section Name Two">
<Description>
<p>Line1 The description text goes here.</p>
<ul><li>Line2 The description text goes here.</li></ul>
</Description>
</Section>
<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>
我在xsl之下使用过标签分组,但是我无法获得output.xml所需的标签。如果我对h1和h2都应用相同的模板,则只能得到xsl以下的h1或h2的结果。
xsl文件:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="xhtml exsl"
version="1.0">
<xsl:import href="http://lenzconsulting.com/xml-to-string/xml-to-string.xsl"/>
<xsl:output method="xml" indent="yes"
cdata-section-elements="MainDescription KeyConsideration"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:key name="h1-group" match="xhtml:div/*[not(self::xhtml:h1)]" use="generate-id(preceding-sibling::xhtml:h1[1])"/>
<xsl:template match="xhtml:div[xhtml:h1]">
<xsl:apply-templates select="xhtml:h1"/>
</xsl:template>
<xsl:template match="xhtml:h1">
<xsl:element name="{translate(., ' ', '')}">
<xsl:variable name="rtf-with-xhtml-ns-stripped">
<xsl:apply-templates select="key('h1-group', generate-id())"/>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($rtf-with-xhtml-ns-stripped)/node()" mode="xml-to-string"/>
</xsl:element>
</xsl:template>
<xsl:template match="xhtml:p">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:2)
我认为您需要为h2
分组定义第二个键
<xsl:key name="h2-group" match="xhtml:div/*[not(self::xhtml:h2)]" use="generate-id(preceding-sibling::xhtml:h2[1])"/>
然后有第二个模板,该模板与组中包含h1
个元素的h2
个元素匹配
<xsl:template match="xhtml:h1[key('h1-group', generate-id())[self::xhtml:h2]]">
<xsl:apply-templates select="key('h1-group', generate-id())[self::xhtml:h2]" />
</xsl:template>
然后在匹配h2
的模板中进行分组
<xsl:template match="xhtml:h2">
<xsl:variable name="h1Group" select="key('h1-group', generate-id(preceding-sibling::xhtml:h1[1]))" />
<Section name="{.}">
<Description>
<xsl:apply-templates select="key('h2-group', generate-id())[count(. | $h1Group) = count($h1Group)]"/>
</Description>
</Section>
</xsl:template>
此处使用count
是为了确保您只选择h2
组中当前h1
之后的元素。
尝试使用此XSLT
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="xhtml exsl"
version="1.0">
<xsl:import href="http://lenzconsulting.com/xml-to-string/xml-to-string.xsl"/>
<xsl:output method="xml" indent="yes" cdata-section-elements="MainDescription KeyConsideration"/>
<xsl:strip-space elements="*"/>
<xsl:key name="h1-group" match="xhtml:div/*[not(self::xhtml:h1)]" use="generate-id(preceding-sibling::xhtml:h1[1])"/>
<xsl:key name="h2-group" match="xhtml:div/*[not(self::xhtml:h2)]" use="generate-id(preceding-sibling::xhtml:h2[1])"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xhtml:div[xhtml:h1]">
<xsl:apply-templates select="xhtml:h1"/>
</xsl:template>
<xsl:template match="xhtml:h1">
<xsl:element name="{translate(., ' ', '')}">
<xsl:choose>
<xsl:when test=". = 'Main Description'">
<xsl:variable name="rtf-with-xhtml-ns-stripped">
<xsl:apply-templates select="key('h1-group', generate-id())"/>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($rtf-with-xhtml-ns-stripped)/node()" mode="xml-to-string"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="key('h1-group', generate-id())"/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
<xsl:template match="xhtml:h1[key('h1-group', generate-id())[self::xhtml:h2]]">
<xsl:apply-templates select="key('h1-group', generate-id())[self::xhtml:h2]" />
</xsl:template>
<xsl:template match="xhtml:h2">
<xsl:variable name="h1Group" select="key('h1-group', generate-id(preceding-sibling::xhtml:h1[1]))" />
<Section name="{.}">
<Description>
<xsl:apply-templates select="key('h2-group', generate-id())[count(. | $h1Group) = count($h1Group)]"/>
</Description>
</Section>
</xsl:template>
<xsl:template match="xhtml:p|xhtml:ul|xhtml:li">
<xsl:element name="{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>