2012-02-22 79 views
1

我试图转换下面的XML ..XSLT提取属性,并生成HTML

<?xml version="1.0" encoding="utf-16"?><Member TextRank="unknown" FullName="My Name" ..etc.. /> 

成类似下面

<div class="member"> 
    <span class="label"> 
     Text Rank (note: i want to override the labels in the xslt) 
    </div> 
    <span class="value"> 
     unknown 
    </span> 
    <span class="label"> 
     Full Name 
    </div> 
    <span class="value"> 
     My Name 
    </span> 
    ..etc.. 
</div> 

我怎么可能,如果能做到这一点使用XSLT?

回答

1

这里有一个不同的方法,即不离开需要对的xsl:选择元素。相反,您可以利用匹配的模板为要覆盖的属性名称以及其他案例的通用模板提供特定的模板。

为了避免代码重复,你还可以使通用模板命名模板,用参数来替代名称

<xsl:template match="@*" name="attribute"> 
    <xsl:param name="label" select="local-name()" /> 

因此,对于大多数属性默认是使用属性名称,但@FullName的特定模板可以用不同的名称调用此模板。

<xsl:template match="@FullName"> 
    <xsl:call-template name="attribute"> 
     <xsl:with-param name="label" select="'Full Name'" /> 
    </xsl:call-template> 
</xsl:template> 

以下是完整的XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" indent="yes"/> 

    <xsl:template match="*"> 
     <div class="{local-name()}"> 
     <div> Title: </div> 
     <xsl:apply-templates select="@*"/> 
     </div> 
    </xsl:template> 

    <xsl:template match="@FullName"> 
     <xsl:call-template name="attribute"> 
     <xsl:with-param name="label" select="'Full Name'" /> 
     </xsl:call-template> 
    </xsl:template> 

    <xsl:template match="@*" name="attribute"> 
     <xsl:param name="label" select="local-name()" /> 
     <span class="label"> 
     <xsl:value-of select="concat($label, ' : ')"/> 
     </span> 
     <span class="value"> 
     <xsl:value-of select="."/> 
     </span> 
     <br/> 
    </xsl:template> 
</xsl:stylesheet> 

当应用于以下XML:

<Member TextRank="unknown" ID="12" FullName="My Name" Dob="01/01/1970" /> 

以下是输出:

<div class="Member"> 
    <div> Title: </div> 
    <span class="label">TextRank : </span> 
    <span class="value">unknown</span> 
    <br> 
    <span class="label">ID : </span> 
    <span class="value">12</span> 
    <br> 
    <span class="label">Full Name : </span> 
    <span class="value">My Name</span> 
    <br> 
    <span class="label">Dob : </span> 
    <span class="value">01/01/1970</span> 
    <br> 
</div> 
0

这是我想出的解决方案。

<?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:template match="*"> 
     <xsl:element name="div"> 
      <xsl:attribute name="class">className</xsl:attribute> 
      <div> 
       Title: 
      </div> 

      <!-- UID, Name, DBO--> 
      <xsl:apply-templates select="@ID"/> 
      <xsl:apply-templates select="@FullName"/> 
      <xsl:apply-templates select="@Dob"/> 

     </xsl:element> 

    </xsl:template> 

    <xsl:template match="@*"> 
     <xsl:element name="span"> 

      <xsl:attribute name="class">label</xsl:attribute> 
      <xsl:choose> 
       <xsl:when test="name() = 'FullName'"> 
        Full Name 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:value-of select="name()"/> 
       </xsl:otherwise> 
      </xsl:choose> 
      : 
     </xsl:element> 
     <xsl:element name="span"> 
      <xsl:attribute name="class">value</xsl:attribute> 
      <xsl:value-of select="."/> 
     </xsl:element> 
     <br/> 
    </xsl:template> 

</xsl:stylesheet> 
+0

为了使您的代码更加清晰,你应该直接写你的元素/属性而不是usi ng'xsl:element'和'xsl:attribute'。 – 2012-02-22 07:52:54