使用XSL转换XML,然后将输出格式化为HTML

时间:2013-01-28 15:50:08

标签: xml xslt xslt-grouping

我需要使用XSL格式化XML输入以获得更方便的结构。作为下一步处理,我想将其转换为HTML。 假设我有以下输入:(0)

<list>
<item item-id="1" second-item-id="1" third-item-id="1"/>
<item item-id="1" second-item-id="1" third-item-id="2"/>
<item item-id="1" second-item-id="2" third-item-id="1"/>
<item item-id="1" second-item-id="3" third-item-id="1"/>

<item item-id="2" second-item-id="1" third-item-id="1"/>
<item item-id="2" second-item-id="1" third-item-id="2"/>
<item item-id="2" second-item-id="1" third-item-id="3"/>
<item item-id="2" second-item-id="2" third-item-id="1"/>

<item item-id="3" second-item-id="1" third-item-id="1"/>
<item item-id="3" second-item-id="1" third-item-id="2"/>
<item item-id="3" second-item-id="1" third-item-id="3"/>
<item item-id="3" second-item-id="1" third-item-id="4"/>
</list>

以及以下XSL模板:(1)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output indent="yes"/>

  <xsl:key name="itemKey" match="item" use="@item-id"/>
  <xsl:key name="secondItemKey" match="item" use="concat(@item-id, '|', @second-item-id)"/>

  <xsl:template match="list">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="item[generate-id() = generate-id(key('itemKey', @item-id)[1])]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="item">
    <item item-id="{@item-id}">
      <xsl:apply-templates select="key('itemKey', @item-id)[generate-id() = generate-id(key('secondItemKey', concat(@item-id, '|', @second-item-id))[1])]" mode="evt"/>
    </item>
  </xsl:template>

  <xsl:template match="item" mode="evt">
    <second-item second-item-id="{@second-item-id}">
      <xsl:apply-templates select="key('secondItemKey', concat(@item-id, '|', @second-item-id))" mode="bus"/>
    </second-item>
  </xsl:template>

  <xsl:template match="item" mode="bus">
    <third-item third-item-id="{@third-item-id}"/>
  </xsl:template>    

</xsl:stylesheet>

它给了我非常好的XML:(2)

<?xml version="1.0"?>
<list>
    <item item-id="1">
        <second-item second-item-id="1">
            <third-item third-item-id="1"/>
            <third-item third-item-id="2"/>
        </second-item>
        <second-item second-item-id="2">
            <third-item third-item-id="1"/>
        </second-item>
        <second-item second-item-id="3">
            <third-item third-item-id="1"/>
        </second-item>
    </item>
    <item item-id="2">
        <second-item second-item-id="1">
            <third-item third-item-id="1"/>
            <third-item third-item-id="2"/>
            <third-item third-item-id="3"/>
        </second-item>
        <second-item second-item-id="2">
            <third-item third-item-id="1"/>
        </second-item>
    </item>
    <item item-id="3">
        <second-item second-item-id="1">
            <third-item third-item-id="1"/>
            <third-item third-item-id="2"/>
            <third-item third-item-id="3"/>
            <third-item third-item-id="4"/>
        </second-item>
    </item>
</list>

我有另一个XSL,它将XML# 2 转换为html:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes" method="html"/>
    <xsl:template match="list">
        <xsl:for-each select="item">
            <h2><xsl:value-of select="concat(local-name(),' ',@item-id)"/></h2>
            <ul>
                <xsl:for-each select="second-item">
                    <li><xsl:value-of select="concat(local-name(),' ',@second-item-id)"/></li>
                    <ul>
                        <xsl:for-each select="third-item">
                            <li><xsl:value-of select="concat(local-name(),' ',@third-item-id)"/></li>
                        </xsl:for-each>
                    </ul>
                </xsl:for-each>
            </ul>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

所以这就是问题:我想在一个步骤中处理带有两个tempaltes(或合并一个)的输入xml。我该怎么做?

提前致谢。

1 个答案:

答案 0 :(得分:1)

如果您对将它们合并在一起感到满意,那么这应该同时完成两者的工作:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output indent="yes"/>

  <xsl:key name="itemKey" match="item" use="@item-id"/>
  <xsl:key name="secondItemKey" match="item" use="concat(@item-id, '|', @second-item-id)"/>

  <xsl:template match="list">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="item[generate-id() = generate-id(key('itemKey', @item-id)[1])]"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="item">
    <h2>
      <xsl:value-of select="concat('item ', @item-id)"/>
    </h2>
    <ul>
      <xsl:apply-templates select="key('itemKey', @item-id)[generate-id() = generate-id(key('secondItemKey', concat(@item-id, '|', @second-item-id))[1])]" mode="evt"/>
    </ul>
  </xsl:template>

  <xsl:template match="item" mode="evt">
    <li>
      <xsl:value-of select="concat('second-item ', @second-item-id)"/>
    </li>
    <ul>
      <xsl:apply-templates select="key('secondItemKey', concat(@item-id, '|', @second-item-id))" mode="bus"/>
    </ul>
  </xsl:template>

  <xsl:template match="item" mode="bus">
    <li>
      <xsl:value-of select="concat('third-item ', @third-item-id)"/>
    </li>
  </xsl:template>

</xsl:stylesheet>

有一种简单的方法可以将它们放在一个XSLT中并一个接一个地运行,但我想到的方法需要使用node-set()函数,不幸的是在不同的命名空间中每个XSLT实现。您使用的是哪个XSLT处理器?