2009-10-12 57 views
1

我有以下XML:XSLT - 如何使用遞歸元素將xml解析到Eclipse toc.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文件,像這樣:

<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,它沒'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> 

你能否提醒一下?

謝謝:)

回答

3

我認爲下面你想要做什麼:

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

嗨嗨嗨:) 謝謝 它的工作原理。 你有好的網站學習XSLT的推薦嗎? – orly 2009-10-12 09:47:40

+0

關於xml的最後一個問題: 如果@value包含字符串,例如:「aaaa#bbb」,我只想要「aaaa」部分? – orly 2009-10-12 09:55:57

+0

'substring-before(@value,'#')' – Tomalak 2009-10-12 10:05:32

1

你的主要問題是這一行:

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

由此產生的所有<LI>元素的列表輸入,從而創建一個平面輸出列表。您可以使用內置的遞歸和「隨大流動」,就像這樣:

<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="BODY"> 
    <toc> 
     <xsl:apply-templates select="OBJECT" /> 
     <xsl:apply-templates select="UL" /> 
    </toc> 
    </xsl:template> 

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

    <xsl:template match="LI"> 
    <topic> 
     <xsl:apply-templates select="OBJECT" /> 
     <xsl:apply-templates select="UL" /> 
    </topic> 
    </xsl:template> 

    <xsl:template match="OBJECT"> 
    <xsl:apply-templates select="param" /> 
    </xsl:template> 

    <xsl:template match="OBJECT/param"> 
    <xsl:variable name="attrName"> 
     <xsl:choose> 
     <xsl:when test="@name = 'FrameName'">label</xsl:when> 
     <xsl:when test="@name = 'Name'">label</xsl:when> 
     <xsl:when test="@name = 'Local'">href</xsl:when> 
     </xsl:choose> 
    </xsl:variable> 
    <xsl:if test="$attrName != ''"> 
     <xsl:attribute name="{$attrName}"> 
     <xsl:value-of select="@value" /> 
     </xsl:attribute> 
    </xsl:if> 
    </xsl:template> 

</xsl:stylesheet> 

產生的輸出是:

<?xml version="1.0" encoding="utf-8"?> 
<toc label="contents"> 
    <topic label="Title1" href="Ref1"> 
    <topic label="Title 2" href="Ref2"> 
     <topic label="Title3" href="Ref3"></topic> 
     <topic label="Title4" href="Ref4"></topic> 
    </topic> 
    <topic label="Title5" href="Ref5"></topic> 
    </topic> 
    <topic label="Title6" href="Ref6"></topic> 
</toc> 

請注意,我換成你的「模式」模板蒙山的<xsl:choose> 。其他的很明顯,我想。