将XHTML(格式良好的XML)无序列表数据处理为分组和排序的XML



我需要将一个XHTML文档(格式良好的XML)转换为标准的XML文档。

输入:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <title>HTML Document Title</title>
  </head>
  <body>
    <h1>Welcome</h1>
    <div class="container">
      <ul>
        <li>
          <a href="a.html" title="abcdef AAA">New York</a>
        </li>
        <li>
          <a href="b.html" title="abcdef AAA">Los Angles</a>
        </li>
        <li>
          <a href="c.html" title="abcdef AAA">Alaska</a>
        </li>
        <li>
          <a href="d.html" title="abcdef BBB">Florida</a>
        </li>
        <li>
          <a href="e.html" title="zyxwvu AAA"><em>California</em></a>
        </li>
      </ul>
    </div>
  </body>
</html>

注意:我注意到使用DOCTYPE声明和简单注释会导致XSLT解析失败。因此,我在XSL解析之前手动删除它们。要正确解析输出,目前使用'xhtml:'前缀,如post中提供的:Can I parse an HTML using XSLT?

根据标签的标题值(子字符串第二部分)对元素进行分组,例如AAA、BBB等。对title属性值的第一部分进行进一步分组(例如abcdef/zyxwvu)或出现标签。总共有四个元素,如; 和& lt; zyxwvu>在输出中。

预期输出:

<root>
    <element title="hard-coded title" href="hard-coded url">
        <element title="AAA" href="AAA.html">
            <abcdef>
                <element title="Alaska" href="c.html">
                <element title="Los Angles" href="b.html">
                <element title="New York" href="a.html">
            </abcdef>
            <zyxwvu>
                <element title="California" href="e.html">
            </zyxwvu>
        </element>
        <element title="BBB" href="BBB.html">
            <abcdef>
                <element title="Florida" href="d.html">
            </abcdef>
        </element>
    </element>
</root>

如果XSLT v1.0 &v2.0 .

这一转换:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:x="http://www.w3.org/1999/xhtml"
 exclude-result-prefixes="x">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:key name="kaByTail" match="x:a"
  use="substring-after(@title, ' ')"/>
 <xsl:key name="kaByHeadAndTail" match="x:a"
  use="concat(substring-before(@title, ' '),
              '+',
              substring-after(@title, ' ')
              )"/>
 <xsl:variable name="vAncors" select="//x:a"/>
 <xsl:template match="/">
  <root>
    <element title="hard-coded title" href="hard-coded url">
     <xsl:for-each select=
      "$vAncors
         [generate-id()
         =
          generate-id(key('kaByTail',
                           substring-after(@title, ' ')
                          )
                           [1]
                      )
         ]">
         <xsl:variable name="vKey"
              select="substring-after(@title, ' ')"/>
         <xsl:variable name="vGroup" select=
         "key('kaByTail', $vKey)"/>
        <element title="{$vKey}" href="{$vKey}.html">
         <xsl:for-each select=
         "$vGroup
            [generate-id()
            =
             generate-id(key('kaByHeadAndTail',
                             concat(substring-before(@title, ' '),
                                   '+',
                                    $vKey
                                    )
                            )
                             [1]
                         )
             ]
         ">
          <xsl:variable name="vKey2"
               select="substring-before(@title, ' ')"/>
          <xsl:element name="{$vKey2}">
           <xsl:for-each select=
            "key('kaByHeadAndTail',
                 concat($vKey2,'+',$vKey)
                 )">
            <xsl:sort/>
            <element title="{.}" href="{@href}"/>
           </xsl:for-each>
          </xsl:element>
         </xsl:for-each>
        </element>
     </xsl:for-each>
    </element>
  </root>
 </xsl:template>
</xsl:stylesheet>

当应用于提供的XML文档时:

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <title>HTML Document Title</title>
  </head>
  <body>
    <h1>Welcome</h1>
    <div class="container">
      <ul>
        <li>
          <a href="a.html" title="abcdef AAA">New York</a>
        </li>
        <li>
          <a href="b.html" title="abcdef AAA">Los Angles</a>
        </li>
        <li>
          <a href="c.html" title="abcdef AAA">Alaska</a>
        </li>
        <li>
          <a href="d.html" title="abcdef BBB">Florida</a>
        </li>
        <li>
          <a href="e.html" title="zyxwvu AAA"><em>California</em></a>
        </li>
      </ul>
    </div>
  </body>
</html>

生成所需的正确结果:

<root>
   <element title="hard-coded title" href="hard-coded url">
      <element title="AAA" href="AAA.html">
         <abcdef>
            <element title="Alaska" href="c.html"/>
            <element title="Los Angles" href="b.html"/>
            <element title="New York" href="a.html"/>
         </abcdef>
         <zyxwvu>
            <element title="California" href="e.html"/>
         </zyxwvu>
      </element>
      <element title="BBB" href="BBB.html">
         <abcdef>
            <element title="Florida" href="d.html"/>
         </abcdef>
      </element>
   </element>
</root>

说明:嵌套的Muenchian分组首先使用单个,然后使用复合分组key。key

最新更新