简体   繁体   中英

XSLT to convert XML into list

Below is my XML

<par def="1">
    <run>
        <font name="Symbol" pitch="variable" truetype="true" familyid="10" />·  </run>
    <run>
        <font name="Calibri" pitch="variable" truetype="true" familyid="20" />abc</run>
</par>

i want to show above data in list. Below is my XSLT

<xsl:template match="run">

<xsl:choose>
  <xsl:when test="@name='Symbol'">
    <xsl:for-each select="ancestor::item">
      <span>
        <xsl:call-template name="style" />
        <xsl:value-of select="current()" />
        <xsl:if test="table">
          <xsl:apply-templates select="table" />
        </xsl:if>
      </span>

    </xsl:for-each>
  </xsl:when>

  <xsl:otherwise>
    <p>
      <span>
        <xsl:call-template name="style" />
        <xsl:value-of select="current()" />
        <xsl:if test="table">
          <xsl:apply-templates select="table" />
        </xsl:if>
      </span>
    </p>
  </xsl:otherwise>
</xsl:choose>

</xsl:template>

it is not working. all par def converted into paragraph not in list.

i want output in HTML like below

<ul>
<li>abc </li>
</ul>

Given:

<?xml version="1.0"?>
<par def="1">
    <run>
        <font name="Symbol" pitch="variable" truetype="true" familyid="10" />·  </run>
    <run>
        <font name="Calibri" pitch="variable" truetype="true" familyid="20" />abc</run>
</par>

Transform using:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*" />

<xsl:template match="/">
  <ul><xsl:apply-templates /></ul>
</xsl:template>

<!-- Remove any "font" elements with a symbol. -->
<xsl:template match="*[font[@name='Symbol']]"></xsl:template>

<xsl:template match="text()">
  <li><xsl:value-of select="normalize-space(.)" /></li>
</xsl:template>

</xsl:stylesheet>

Produces:

<ul>
  <li>abc</li>
</ul>

Quite often, using xsl:choose or xsl:if will result in more complex code than using xsl:apply-templates and xsl:template match . The if statements are typically a sign of procedural programming, rather than template programming. With template matching, conditionals are expressed as part of the match attribute (eg, @name='Symbol' ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM