2012-10-25 4 views
1

XSL 문에 다른 문제가 있습니다. 나는 특정 값이 발견되면이 인스턴스 'call'에서 약간 다르게 동작하도록 조건문을 추가하려고합니다. 나는 내가 잘못하고있는 간단한 일이지만 어떤 생각이 없는지 안다!xsl for-each 문과 관련된 문제

기본적으로 아래 코드는 올바른 부울 값을 반환하는 것으로 보이지만 호출 비용 문구는 그대로 둡니다. 클래스 및 URL 위치는 모두 올바르게 출력됩니다.

미리 감사드립니다. XML

<rhs_options> 
<title>RHS title</title> 
<service> 
    <option>call</option> 
    <text>telephone number text</text> 
    <telephone>telephone number</telephone> 
    <link>telephone number link</link> 
</service> 
<service> 
    <option>branch</option> 
    <text>branch text</text> 
    <telephone/> 
    <link>branch link</link> 
</service> 
<service> 
    <option>online</option> 
    <text>online text</text> 
    <telephone/> 
    <link>online link</link> 
</service> 

XSL

<aside class="cta"> 
<h4><xsl:value-of select="/Properties/Data/Datum[@ID='ID1']/DCR[@Type='RHS_Options']/rhs_options/title"/></h4> 
    <xsl:for-each select="/Properties/Data/Datum[@ID='ID1']/DCR[@Type='RHS_Options']/rhs_options/service"> 
     <xsl:choose> 
      <xsl:when test="/Properties/Data/Datum[@ID='ID1']/DCR[@Type='RHS_Options']/rhs_options/service/option='call'"> 
       <p class="{./option}"> 
        <xsl:value-of select="./text"/> 
         <br/> 
        <xsl:value-of select="./telephone"/> 
         <br/> 
        <a href="{./link}"> 
         Call charges 
        </a> 
       </p> 
      </xsl:when> 
      <xsl:when test="/Properties/Data/Datum[@ID='ID1']/DCR[@Type='RHS_Options']/rhs_options/service/option='phone'or'online'"> 
       <p class="{./option}"> 
        <a href="{./link}"> 
         <xsl:value-of select="./text"/> 
        </a> 
       </p> 
      </xsl:when> 
     </xsl:choose> 
    </xsl:for-each> 

+0

또한 에 대한 마지막 을 변경하려고했지만 아무런 차이가 없었습니다. – Barlow1984

답변

1

당신은 원하는 :

<xsl:choose> 
     <xsl:when test="option='call'"> 
      <p class="{option}"> 
       <xsl:value-of select="text"/> 
        <br/> 
       <xsl:value-of select="telephone"/> 
        <br/> 
       <a href="{link}"> 
        Call charges 
       </a> 
      </p> 
     </xsl:when> 
     <xsl:when test="option='phone' or option = 'online'"> 
      <p class="{option}"> 
       <a href="{link}"> 
        <xsl:value-of select="text"/> 
       </a> 
      </p> 
     </xsl:when> 
    </xsl:choose> 

설명 : 제공된 코드

문제 절대 XPath 식을 사용하는 것이이지만 상대 XPath 식을 사용할 필요가있다. 명시 적 XSLT 조건부 명령어를 사용하는을위한-각 * 및 하지 :

물론, 는`XSL을 사용하지 좋습니다. 여기

은 동일하지만, 훨씬 더 콤팩트하고 유지 관리되는 코드이다 원하는 결과가 생성된다

<rhs_options> 
    <title>RHS title</title> 
    <service> 
     <option>call</option> 
     <text>telephone number text</text> 
     <telephone>telephone number</telephone> 
     <link>telephone number link</link> 
    </service> 
    <service> 
     <option>branch</option> 
     <text>branch text</text> 
     <telephone/> 
     <link>branch link</link> 
    </service> 
    <service> 
     <option>online</option> 
     <text>online text</text> 
     <telephone/> 
     <link>online link</link> 
    </service> 
</rhs_options> 

이 변환이 제공된 XML 문서에 적용

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

<xsl:template match="/*"> 
    <aside class="cta"> 
     <h4><xsl:value-of select="title"/></h4> 
     <xsl:apply-templates/> 
    </aside> 
</xsl:template> 

<xsl:template match="service[option='call']"> 
    <p class="{option}"> 
    <xsl:value-of select="text"/> 
    <br/> 
    <xsl:value-of select="telephone"/> 
    <br/> 
    <a href="{link}"> 
     Call charges 
    </a> 
    </p> 
</xsl:template> 

<xsl:template match="service"> 
    <p class="{option}"> 
    <a href="{link}"> 
     <xsl:value-of select="text"/> 
    </a> 
    </p> 
</xsl:template> 
</xsl:stylesheet> 

:

<aside class="cta"> 
    <h4>RHS title</h4>RHS title<p class="call">telephone number text<br/>telephone number<br/> 
     <a href="telephone number link"> 
     Call charges 
    </a> 
    </p> 
    <p class="branch"> 
     <a href="branch link">branch text</a> 
    </p> 
    <p class="online"> 
     <a href="online link">online text</a> 
    </p> 
</aside> 
+0

대단히 감사합니다. 정말 고마워요. 그것은 훨씬 더 명확하게 해줍니다! – Barlow1984

+0

@ Barlow1984, 오신 것을 환영합니다. –