0

我必须在下面的 xml 中添加一个 href 属性,但不是每个子进程都有一个。

<?xml version="1.0"?>
<processes>
    <process nr="1" name="Process1" >
        <subprocess nr="1" name="Subprocess1" href="some/relative/link.html"></subprocess>
        <subprocess nr="2" name="Subprocess2" href="#"></subprocess>
        <subprocess nr="3" name="Subprocess3"></subprocess>
    </process>
</processes>

我需要在我的原始 XSL 中进行哪些更改才能生成以下 HTML 输出?如何处理不同的 href 属性值?

XSL

<xsl:template name="subprocess">
<xsl:call-template name="linking"/>
</xsl:template>
 <xsl:template name="linking">
 <xsl:variable name="processNo" select="@nr"/>   
 <ul class="myframe">
  <xsl:for-each select="subprocess">
    <li>
      <a class="myButton" href="p{$processNo}s{@nr}.html">
        <xsl:value-of select="@nr"/>
        <br/>
        <xsl:value-of select="@name"/>
      </a>
    </li>
  </xsl:for-each>
</ul>
</xsl:template>

HTML:

<ul class="myframe">
    <li><a class="linkButton" href="some/relative/link.html">Subprocess1</a></li>
    <li><a class="noLink" href="#">Subprocess2</a></li>
    <li><a class="myButton" href="p1s3.html">Subprocess3</a></li>
</ul>

因此,根据 href 属性,我也想分配不同的 CSS 类。

谢谢你的帮助。

4

2 回答 2

1

Another way to approach this is to make use of power of pattern matching with templates. You have a separate template to match the various conditions that occur for your subprocess elements.

<xsl:template match="subprocess[@href = '#']">

<xsl:template match="subprocess[@href != '' and @href != '#']">

<xsl:template match="subprocess">

Then, within each one of these you could call your linking template, but passing in the relevant class, and optionally the link, as a parameter. For example...

<xsl:template match="subprocess[@href = '#']">
   <xsl:call-template name="linking">
      <xsl:with-param name="class" select="'noLink'"/>
   </xsl:call-template>
</xsl:template>

Here is the full XSLT

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

   <xsl:template match="process">
      <ul class="myframe">
         <xsl:apply-templates select="subprocess"/>
      </ul>
   </xsl:template>

   <xsl:template match="subprocess[@href = '#']">
      <xsl:call-template name="linking">
         <xsl:with-param name="class" select="'noLink'"/>
      </xsl:call-template>
   </xsl:template>

   <xsl:template match="subprocess[@href != '' and @href != '#']">
      <xsl:call-template name="linking" />
   </xsl:template>

   <xsl:template match="subprocess">
      <xsl:call-template name="linking">
         <xsl:with-param name="class" select="'myButton'"/>
         <xsl:with-param name="link" select="concat('p', ../@nr, 's', @nr, '.html')"/>
      </xsl:call-template>
   </xsl:template>

   <xsl:template name="linking">
      <xsl:param name="class" select="'linkButton'" />
      <xsl:param name="link" select="@href"/>
      <li>
         <a class="{$class}" href="{$link}">
            <xsl:value-of select="@name"/>
         </a>
      </li>
   </xsl:template>
</xsl:stylesheet>

When applied to your XML, the following is output

<ul class="myframe">
   <li>
      <a class="linkButton" href="some/relative/link.html">Subprocess1</a>
   </li>
   <li>
      <a class="noLink" href="#">Subprocess2</a>
   </li>
   <li>
      <a class="myButton" href="p1s3.html">Subprocess3</a>
   </li>
</ul>
于 2012-11-16T22:12:31.150 回答
1

使用xsl:choosexsl:attribute

<xsl:template name="linking">
    <xsl:variable name="processNo" select="@nr"/>
    <ul class="myframe">
        <xsl:for-each select="subprocess">
            <li>
                <a>
                    <xsl:choose>
                        <xsl:when test="@href eq '#'">
                            <xsl:attribute name="class" select="'noLink'"/>
                        </xsl:when>
                        <xsl:when test="not(@href)">
                            <xsl:attribute name="class" select="'myButton'"/>
                            <xsl:attribute name="href"
                                select="concat('p',$processNo,'s',@nr,'.html')"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:sequence select="@href"/>
                        </xsl:otherwise>
                    </xsl:choose>
                    <xsl:value-of select="name()"/>
                </a>
            </li>
        </xsl:for-each>
    </ul>
</xsl:template>
于 2012-11-16T19:28:02.463 回答