Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 条件上的XSL转换_Xml_Xslt - Fatal编程技术网

Xml 条件上的XSL转换

Xml 条件上的XSL转换,xml,xslt,Xml,Xslt,我必须向以下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=

我必须向以下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>

要生成下面的HTML输出,我需要在原始XSL中更改什么? 如何处理不同的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类


感谢您的帮助。

使用
xsl:choose
xsl: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>


另一种方法是利用模板的模式匹配功能。您有一个单独的模板来匹配子流程元素出现的各种条件

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

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

<xsl:template match="subprocess">

然后,在其中的每一个模板中,您都可以调用链接模板,但可以将相关类和链接作为参数传入。例如

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

这是完整的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>

  • 应用于XML时,将输出以下内容

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