programing

if-else 문을 XSLT에서 어떻게 구현합니까?

iphone6s 2023. 10. 24. 20:15
반응형

if-else 문을 XSLT에서 어떻게 구현합니까?

if-else 문을 XSLT에 구현하려고 하는데 코드가 파싱되지 않습니다.생각나는 사람 있습니까?

  <xsl:variable name="CreatedDate" select="@createDate"/>
  <xsl:variable name="IDAppendedDate" select="2012-01-01" />
  <b>date: <xsl:value-of select="$CreatedDate"/></b> 

  <xsl:if test="$CreatedDate > $IDAppendedDate">
    <h2> mooooooooooooo </h2>
  </xsl:if>
  <xsl:else>
    <h2> dooooooooooooo </h2>
  </xsl:else>

를 사용하여 다시 구현해야 합니다.<xsl:choose>태그:

<xsl:choose>
  <xsl:when test="$CreatedDate > $IDAppendedDate">
    <h2> mooooooooooooo </h2>
  </xsl:when>
  <xsl:otherwise>
    <h2> dooooooooooooo </h2>
  </xsl:otherwise>
</xsl:choose>

한 가지 조건만 빠르게 확인할 때 문을 사용하는 경우.옵션이 여러 개일 경우 사용<xsl:choose>아래 그림과 같이:

   <xsl:choose>
     <xsl:when test="$CreatedDate > $IDAppendedDate">
       <h2>mooooooooooooo</h2>
     </xsl:when>
     <xsl:otherwise>
      <h2>dooooooooooooo</h2>
     </xsl:otherwise>
   </xsl:choose>

또한 여러 개를 사용할 수 있습니다.<xsl:when>표현할 태그If .. Else If아니면Switch아래 그림과 같은 패턴:

   <xsl:choose>
     <xsl:when test="$CreatedDate > $IDAppendedDate">
       <h2>mooooooooooooo</h2>
     </xsl:when>
     <xsl:when test="$CreatedDate = $IDAppendedDate">
       <h2>booooooooooooo</h2>
     </xsl:when>
     <xsl:otherwise>
      <h2>dooooooooooooo</h2>
     </xsl:otherwise>
   </xsl:choose>

앞의 예는 아래의 의사 코드와 같습니다.

   if ($CreatedDate > $IDAppendedDate)
   {
       output: <h2>mooooooooooooo</h2>
   }
   else if ($CreatedDate = $IDAppendedDate)
   {
       output: <h2>booooooooooooo</h2>
   }
   else
   {
       output: <h2>dooooooooooooo</h2>
   }

가 몇 가지 제안을 할 수 있다면 (2년 후이지만 미래의 독자들에게 도움이 되기를 바랍니다):

  • 공통 요소를 제거합니다.h2요소.
  • 공통 요소를 제거합니다.ooooooooooooo본문.
  • 새로운 XPath 2.0에 유의해야 합니다.if/then/elseXSLT 2.0을 사용하는 경우 구성합니다.

XSLT 1.0 솔루션

<h2>
  <xsl:choose>
    <xsl:when test="$CreatedDate > $IDAppendedDate">m</xsl:when>
    <xsl:otherwise>d</xsl:otherwise>
  </xsl:choose>
  ooooooooooooo
</h2>

XSLT 2.0 솔루션

<h2>
   <xsl:value-of select="if ($CreatedDate > $IDAppendedDate) then 'm' else 'd'"/>
   ooooooooooooo
</h2>

원래 이 블로그 게시물에서.우리는 아래 코드를 사용하여 다른 경우에 달성할 수 있습니다.

<xsl:choose>
    <xsl:when test="something to test">

    </xsl:when>
    <xsl:otherwise>

    </xsl:otherwise>
</xsl:choose>

제가 한 일은 이렇습니다.

<h3>System</h3>
    <xsl:choose>
        <xsl:when test="autoIncludeSystem/autoincludesystem_info/@mdate"> <!-- if attribute exists-->
            <p>
                <dd><table border="1">
                    <tbody>
                        <tr>
                            <th>File Name</th>
                            <th>File Size</th>
                            <th>Date</th>
                            <th>Time</th>
                            <th>AM/PM</th>
                        </tr>
                        <xsl:for-each select="autoIncludeSystem/autoincludesystem_info">
                            <tr>
                                <td valign="top" ><xsl:value-of select="@filename"/></td>
                                <td valign="top" ><xsl:value-of select="@filesize"/></td>
                                <td valign="top" ><xsl:value-of select="@mdate"/></td>
                                <td valign="top" ><xsl:value-of select="@mtime"/></td>
                                <td valign="top" ><xsl:value-of select="@ampm"/></td>
                            </tr>
                        </xsl:for-each>
                    </tbody>
                </table>
                </dd>
            </p>
        </xsl:when>
        <xsl:otherwise> <!-- if attribute does not exists -->
            <dd><pre>
                <xsl:value-of select="autoIncludeSystem"/><br/>
            </pre></dd> <br/>
        </xsl:otherwise>
    </xsl:choose>

내 출력

enter image description here

가장 간단한 방법은 조건을 반전시켜 두 번째 if-test를 수행하는 것입니다.이 기법은 중첩된 블록을 선택할 때보다 눈에 더 짧고 눈에 더 쉽고 오른쪽으로 이동하기도 쉽습니다.

<xsl:variable name="CreatedDate" select="@createDate"/>
     <xsl:variable name="IDAppendedDate" select="2012-01-01" />
     <b>date: <xsl:value-of select="$CreatedDate"/></b> 
     <xsl:if test="$CreatedDate &gt; $IDAppendedDate">
        <h2> mooooooooooooo </h2>
     </xsl:if>
     <xsl:if test="$CreatedDate &lt;= $IDAppendedDate">
        <h2> dooooooooooooo </h2>
     </xsl:if>

다음은 정부 웹 사이트의 스타일시트에 사용되는 기술의 실제 예입니다. http://w1.weather.gov/xml/current_obs/latest_ob.xsl

언급URL : https://stackoverflow.com/questions/13622338/how-to-implement-if-else-statement-in-xslt

반응형