【发布时间】:2012-02-01 11:20:52
【问题描述】:
我有一个 HTML 文件,我想只使用 XSLT 转换为 XML..
我想要 1.所有要保留的节点。 2.所有元素都已排序。 3.并且代码应该是动态的。
我有一个巨大的文件,所以我想要一个简单的代码来处理所有的 html 节点。这里我解释了我使用 xslt 的编码 如果你理解它请帮助我..
我更新的 HTML 文件是..
<div id="2196" class="one_tail">
<span id="2197" class="one_biblio">
<span id="2198" class="one_section-title"><b>Title</b></span>
<ol id="2199" class="one_biblio-sec">
<li id="2200" class="one_bib-reference">
<span id="2202" class="one_reference">
<span id="2203" class="one_contribution">
<span id="2204" class="one_authors">
<span id="2205" class="one_author">
<!-- here the id value misplaced -->
<span id="2207" class="one_surname">Surname</span>
<span id="2206" class="one_given-name">GivenName</span>
</span>
</span>
<span id="2208" class="one_title">
<span id="2209" class="one_maintitle">technology</span>
</span>
</span>
<span id="2210" class="one_host">
<span id="2211" class="one_book">
<span id="2213" class="one_publisher">Publisher </span>
</span>
</span>.
</span>
</li>
</ol>
</span>
</div>
我希望 XML 文件为: 这里使用属性类值作为元素名称。
<tail id="2196">
<biblio id="2197" >
<section-title id="2198" ><b>Title</b></section-title>
<biblio-sec id="2199" >
<bib-reference id="2200" >
<reference id="2202" >
<contribution id="2203" >
<authors id="2204" >
<author id="2205" >
<!-- correrct the id -->
<given-name id="2206" >GivenName </given-name>
<surname id="2207" >Surname</surname>
</author>
</authors>
<title id="2208" >
<maintitle id="2209" >technology</maintitle>
</title>
</contribution>
</reference>
<host id="2210" >
<book id="2211" >
<publisher id="2213" >Publisher </publisher>
</book>
</host>
</bib-reference>
</biblio-sec>
</biblio>
</tail>
我写的 XSLT 没有给出我想要的。 XSLT 代码是:
<xsl:template match="*|/"
<xsl:for-each select=".">
<xsl:for-each select="current()/*">
<xsl:sort select="@id" order="ascending"/>
</xsl:for-each>
<xsl:if test="starts-with(@class,'one_') ">
<xsl:variable name="nodename" select="substring-after(@class,'one_')"/>
<xsl:element name="{$nodename}">
<xsl:attribute name="id" select="@id"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:if>
<xsl:if test="not(@class)">
<xsl:apply-templates/>
</xsl:if>
</xsl:for-each>
</xsl:template>
谁能帮帮我..
【问题讨论】: