我有一个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>
任何人都可以帮助我..
答案 0 :(得分:1)
以下是样式表示例:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[@class]">
<xsl:element name="{substring-after(@class, 'one_')}">
<xsl:copy-of select="@id"/>
<xsl:apply-templates select="*">
<xsl:sort select="@id" data-type="number"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:1)
此转化:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[@class]">
<xsl:element name="{substring-after(@class, 'one_')}">
<xsl:copy-of select="@*[not(name()='class')]"/>
<xsl:apply-templates>
<xsl:sort select="@id" data-type="number"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
应用于提供的XML文档:
<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>
生成想要的正确结果:
<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"><!-- here the id value misplaced -->
<given-name id="2206">GivenName</given-name>
<surname id="2207">Surname</surname>
</author>
</authors>
<title id="2208">
<maintitle id="2209">technology</maintitle>
</title>
</contribution>
<host id="2210">
<book id="2211">
<publisher id="2213">Publisher </publisher>
</book>
</host>
</reference>
</bib-reference>
</biblio-sec>
</biblio>
</tail>