【问题标题】:Process XHTML (well-formatted XML) unordered list data to grouped and sorted XML将 XHTML(格式良好的 XML)无序列表数据处理为分组和排序的 XML
【发布时间】:2011-09-20 13:46:08
【问题描述】:

我需要将 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>

注意:我注意到在 XSLT 解析过程中使用 DOCTYPE 声明和简单的 cmets 会导致失败。因此,我在 XSL 解析之前手动删除了它们。为了正确解析输出,目前使用帖子中提供的“xhtml:”前缀:Can I parse an HTML using XSLT?

根据标签标题值(子字符串第二部分)对元素进行分组,例如AAA, BBB, etc. 进一步分组在第 1 部分的 title 属性值(例如 abcdef / 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 都提供解决方案将不胜感激。

【问题讨论】:

  • 如果你努力,人们会更愿意帮助你。您尝试了什么,什么不起作用?您提出问题的方式,就好像您希望响应者提供整个解决方案。
  • 是的,我需要有人帮助才能获得整个解决方案。我尝试将密钥设为&lt;xsl:key name="keyId" match="xhtml:a" use="substring-after(xhtml:li/xhtml:a/@title, ' ')" /&gt;,但无法为分组目的定义 generate-id() 函数。

标签: xml xslt


【解决方案1】:

这种转变

<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

【讨论】:

  • 惊人的回复 Dimitre Novatchev !!
猜你喜欢
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-26
  • 1970-01-01
  • 2016-11-10
  • 2010-11-08
相关资源
最近更新 更多