2011-08-01 5 views
0

문의하고 싶습니다. 조회 테이블을 통해 xml 데이터를 검사 할 조건을 추가 할 수 있습니다. 조회 테이블에 값이 없으면 출력에 const 8을 추가 할 수 있습니까? XSLT 코드 :조회 테이블 xml 데이터 확인 조건을 추가하는 방법

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:key name="Department" match="Department" use="../Collection"/> 
    <xsl:template match="/"> 
    <document> 
     <xsl:apply-templates/> 
    </document> 
</xsl:template> 
<xsl:template match="line"> 
    <xsl:variable name="inputDep" select="field[@id='3']"/> 
<Department> 
    <xsl:for-each select="document('lookup.xml')"> 
     <xsl:for-each select="key('Deparment',$inputDep)"> 
      <xsl:value-of select="."/> 
     </xsl:for-each> 
    </xsl:for-each> 
</Department> 

</xsl:template> 

</xsl:stylesheet> 

조회 테이블 :

<document> 
    <line-item> 
     <Collection>1</Collection> 
     <Department>3</Department> 
    </line-item> 
    <line-item> 
     <Collection>2</Collection> 
     <Department>1</Department> 
    </line-item> 
    <line-item> 
     <Collection>3</Collection> 
     <Department>2</Department> 
    </line-item> 
</document> 

XML 파일 :

<document> 
    <line id="0"> 
     <field id="3"><![CDATA[1]]></field> 
    </line> 
    <line id="1"> 
     <field id="3"/> 
    </line> 
    <line id="2"> 
     <field id="3"/><![CDATA[4]]></field> 
    </line> 
</document> 

결과 :

<Department>3<Department> 
<Department>8<Department> 
<Department>8<Department> 
+0

왜 내부 for-each 루프입니까? Deparment (sic) 키를 사용하는 조회가 둘 이상의 노드를 리턴 할 것으로 예상합니까? 그렇다면 출력이 올바를 것인가? –

답변

1

당신은에 보였다 최대 값을 할당 할 수 변수를 선택하고 무엇을 선택할 것인가? 무엇이 발견되었는지에 따라 출력.

편집 2 : 전체 데모 스타일 :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:key name="Department" match="Department" use="../Collection"/> 

    <xsl:template match="/"> 
    <document> 
     <xsl:apply-templates/> 
    </document> 
    </xsl:template> 

    <xsl:template match="line"> 
    <xsl:variable name="inputDep" select="field[@id='3']"/> 
    <Department> 
     <xsl:for-each select="document('lookup.xml')"> 
     <xsl:variable name="value" select="key('Department',$inputDep)"/> 
     <xsl:choose> 
      <xsl:when test="$value"> 
      <xsl:value-of select="$value"/> <!-- see note --> 
      </xsl:when> 
      <xsl:otherwise>8</xsl:otherwise> 
     </xsl:choose> 
     </xsl:for-each> 
    </Department> 
    </xsl:template> 

</xsl:stylesheet> 

참고 : 루핑 값이 의도적이 아니었다 가정, 간단한 xsl:value-of에 원래 스타일 시트에 xsl:for-each 루프를 대체되었습니다. 그것이 실제로 있다면, 이것을 for-each 루프로 대체 할 수 있습니다.

+0

xml 파일이 아닌 조회 테이블 용 각각에 대해 내부. 귀하의 답변을 사용하려고하지만 작동하지 않습니다 .. 각 루프 대신 코드를 추가하려고합니다. – Petras

+0

@Petras는 내 마음에 무엇이 있었는지 보여주는 완벽한 예제 코드를 제공하여 내 대답을 명확히하려고 노력했습니다. –

+0

당신의 해결책을 시도해 봅니다. 우선, 행에 오류가 있음을 보여줍니다./field [@ id = 3]에 대한 선택 값을 변경하려고 시도하지만 모든 부서 = 8 – Petras

관련 문제