【问题标题】:Dynamically Sort nodes by id and process each node using XSLT按 id 动态排序节点并使用 XSLT 处理每个节点
【发布时间】: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>   

谁能帮帮我..

【问题讨论】:

    标签: html xml xslt


    【解决方案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>
    

    【讨论】:

    • 谢谢..非常感谢 Dimitre.. 它工作得很好...我还有一个疑问 - 我在父标签之外有一些值,所以这些值出现在孩子之前如何躲开它。在 html 中,我有 ,one,,two,, 这是 ,,,,,one两个我不想避免那些逗号的(,,,,,)怎么办....?请帮助我....
    • @Ramesh:不客气。至于您迄今为止未知的新需求,有两种方法可以解决此问题:使用 Update 编辑您当前的问题,或者,最好提出一个新问题并提供您的新 XML 文档和所需输出的任何新要求。根据我的经验,如果对问题进行编辑并添加新要求,问题就会变得过于繁重。当您发布新问题时,我很乐意阅读并提供答案。
    【解决方案2】:

    这是一个示例样式表:

    <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>
    

    【讨论】:

    • 它给出了一些重复的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    相关资源
    最近更新 更多