XSLT - 如何将带有递归元素的 xml 解析为 Eclipse toc.xml? [英] XSLT - how to parse xml with recursive elements to Eclipse toc.xml?

查看:60
本文介绍了XSLT - 如何将带有递归元素的 xml 解析为 Eclipse toc.xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XML:

<HTML>
  <HEAD>
    <META name="GENERATOR" content="Microsoft HTML Help Workshop 4.1" />
    <!-- Sitemap 1.0 -->
  </HEAD>
  <BODY>
    <OBJECT type="text/site properties">
      <param name="FrameName" value="contents" />
    </OBJECT>
    <UL>
      <LI>
        <OBJECT type="text/sitemap">
          <param name="Name" value="Title1" />
          <param name="Local" value="Ref1" />
        </OBJECT>
        <UL>
          <LI>
            <OBJECT type="text/sitemap">
              <param name="Name" value="Title 2" />
              <param name="Local" value="Ref2" />
            </OBJECT>
            <UL>
              <LI>
                <OBJECT type="text/sitemap">
                  <param name="Name" value="Title3" />
                  <param name="Local" value="Ref3" />
                </OBJECT>
              </LI>
              <LI>
                <OBJECT type="text/sitemap">
                  <param name="Name" value="Title4" />
                  <param name="Local" value="Ref4" />
                </OBJECT>
              </LI>
            </UL>
          </LI>
          <LI>
            <OBJECT type="text/sitemap">
              <param name="Name" value="Title5" />
              <param name="Local" value="Ref5" />
            </OBJECT>
          </LI>
        </UL>
      </LI>
      <LI>
        <OBJECT type="text/sitemap">
          <param name="Name" value="Title6" />
          <param name="Local" value="Ref6" />
        </OBJECT>
      </LI>
    </UL>
  </BODY>
</HTML>

我需要将其转换为Eclipse 帮助"格式的 toc.xml 文件,如下所示:

I need to transform it to an "Eclipse Help" format toc.xml file, like this:

<toc label="Sample Table of Contents">
  <topic label="Title1"  href="Ref1"> 
    <topic label="Title2" href="Ref2">
      <topic label="Title3" href="Ref3"/>
        <topic label="Title4" href="Ref4"/>
      </topic> 
      <topic label="Title5" href="Ref5">
    </topic> 
  </topic>
  <topic label="Title6" href="Ref6"/>
</toc>

我尝试创建以下 XSLT,但没有成功:

I tried to create the following XSLT, which didn't work:

<?xml version="1.0" encoding="utf-8"?>
<?altova_samplexml D:\Documents and Settings\oshecht\Desktop\XSL\Copy of toc.xml?>
<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

  <xsl:template match="/">
    <toc>
      <xsl:apply-templates select="//LI" />
    </toc>
  </xsl:template>

  <xsl:template match="//LI">
    <topic>
      <xsl:apply-templates select="OBJECT/param" mode="val" />
      <xsl:apply-templates select="OBJECT/param" mode="ref" />
      <xsl:apply-templates select="/UL/LI" />
      <!--xsl:apply-templates select="//UL//LI"  mode="subelement" /-->
    </topic>
  </xsl:template>

  <xsl:template match="OBJECT/param" mode="val">
    <xsl:if test="@name = 'Name'">
      <xsl:attribute name="label">
        <xsl:value-of select="@value" />
      </xsl:attribute>
    </xsl:if>
  </xsl:template>

  <xsl:template match="OBJECT/param" mode="ref">
    <xsl:if test="@name = 'Local'">
      <xsl:attribute name="href">
        <xsl:value-of select="@value" />
      </xsl:attribute>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

你能指点一下吗?

谢谢:)

推荐答案

我认为以下内容可以满足您的需求:

I think the following does what you want:

<xsl:template match="BODY">
    <toc label="Sample Table of Contents">
        <xsl:apply-templates select="UL/LI/OBJECT"/>
  </toc>
</xsl:template>

<xsl:template match="OBJECT">
  <topic label="{param[@name='Name']/@value}" href="{param[@name='Local']/@value}">
    <xsl:apply-templates select="following-sibling::UL/LI/OBJECT"/>
  </topic>
</xsl:template>

这篇关于XSLT - 如何将带有递归元素的 xml 解析为 Eclipse toc.xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆