2009-05-22 5 views

답변

12

한 가지 방법은 스타일 시트로 상태 데이터를 포함하는 것입니다

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:my="whatever" 
    exclude-result-prefixes="my"> 

    <xsl:output indent="yes"/> 

    <!-- The value of the state you want to select, supplied in the input XML --> 
    <xsl:variable name="selected-state" select="/xpath/to/state/value"/> 

    <!-- You have to use a namespace, or the XSLT processor will complain --> 
    <my:states> 
    <option>Alabama</option> 
    <option>Alaska</option> 
    <!-- ... --> 
    <option>Wisconsin</option> 
    <option>Wyoming</option> 
    </my:states> 

    <xsl:template match="/"> 
    <!-- rest of HTML --> 
    <select name="state"> 
     <!-- Access the embedded document as an internal "config" file --> 
     <xsl:apply-templates select="document('')/*/my:states/option"/> 
    </select> 
    <!-- rest of HTML --> 
    </xsl:template> 

      <!-- Copy each option --> 
      <xsl:template match="option"> 
      <xsl:copy> 
       <!-- Add selected="selected" if this is the one --> 
       <xsl:if test=". = $selected-state"> 
       <xsl:attribute name="selected">selected</xsl:attribute> 
       </xsl:if> 
       <xsl:value-of select="."/> 
      </xsl:copy> 
      </xsl:template> 

</xsl:stylesheet> 

당신은 어떤 질문이 있으면 알려주세요 다음과 같이 자체와는 document('')를 사용하여 스타일 시트 문서에 액세스 할 수 있습니다.

+1

my : states 노드를 변수 선언 안에 넣고 select 식에서이 변수를 사용할 수도 있습니까? –

+1

XSLT 2.0에서 예. XSLT 1.0에서는 exsl : node-set() 또는 msxsl : node-set()과 같은 확장 함수를 사용해야합니다. 문서 ('') 솔루션은 어느 것도 필요로하지 않습니다. –

1

이상적으로 당신은 당신의 XML 파일에서 국가의 목록을 저장 것이며, 단지 그들을 반복하는 XSLT를 사용) 더 많은 아이디어를 환영합니다.

업데이트 : 당신이 XML을 편집 할 수없는 경우, 두 번째 데이터 파일에서 데이터를로드 할 document function를 사용하여 볼 수 있었다 :이 작업을 수행하는

+0

XML을 변경할 수 없으며 다른 시스템에서 제공합니다. –

+0

문서 기능을 사용하여 상태 목록이 포함 된 다른 정적 XML 문서에 계속 액세스 할 수 있습니까? – Elijah

관련 문제