2009-08-14 3 views
5

Saxon 9.1.0.7을 사용하여 간단한 외부 조회를 사용하려고 할 때 내 머리카락을 남겨 두었습니다.XSLT 2.0 key() 및 document()를 사용하는 외부 조회

<something> 
    <monkey> 
     <genrecode>AAA</genrecode> 
    </monkey> 
    <monkey> 
     <genrecode>BBB</genrecode> 
    </monkey> 
    <monkey> 
     <genrecode>ZZZ</genrecode> 
    </monkey> 
    <monkey> 
     <genrecode>ZER</genrecode> 
    </monkey> 
</something> 

그런 다음 조회 파일이 GenreSet_124.xml입니다 :

나는 간단한 소스 파일 dummy.xml이 내가 '무엇

<GetGenreMappingObjectsResponse> 
    <tuple> 
     <old> 
     <GenreMapping DepartmentCode="AAA" 
         DepartmentName="AND - NEWS AND CURRENT AFFAIRS" 
         Genre="10 - NEWS"/> 
     </old> 
    </tuple> 
    <tuple> 
     <old> 
     <GenreMapping DepartmentCode="BBB" 
         DepartmentName="AND - NEWS AND CURRENT AFFAIRS" 
         Genre="11 - NEWS"/> 
     </old> 
    </tuple> 

    ... lots more 
</GetGenreMappingObjectsResponse> 

성취하려고 노력하는 것은 단순히 "장르 "값을"DepartmentCode "값을 기반으로합니다.

... 
<!-- Set up the genre lookup key --> 
<xsl:key name="genre-lookup" match="GenreMapping" use="@DepartmentCode"/> 


<xsl:variable name="lookupDoc" select="document('GenreSet_124.xml')"/> 

<xsl:template match="/something"> 
<stuff> 
    <xsl:for-each select="monkey"> 
     <Genre> 

     <xsl:apply-templates select="$lookupDoc"> 
     <xsl:with-param name="curr-label" select="genrecode"/> 
     </xsl:apply-templates> 

     </Genre> 
    </xsl:for-each> 
</stuff> 
</xsl:template> 

<xsl:template match="GetGenreMappingObjectsResponse"> 
    <xsl:param name="curr-genrecode"/> 

    <xsl:value-of select="key('genre-lookup', $curr-genrecode)/@Genre"/> 

</xsl:template> 

...


내가 가진 문제는 내가 다시 아무것도 얻을 수 없다는 것입니다 : 같은

그래서 내 XSL 보인다. 내가 GenreMapping의 속성으로 모든 조회 데이터를 이동 한

<?xml version="1.0" encoding="UTF-8"?> 
<stuff> 
    <Genre/> 
    <Genre/> 
    <Genre/> 
    <Genre/> 
</stuff> 

나는 "GetGenreMappingObjectsResponse"그냥 모든 GenreMapping에서 모든 텍스트를 인쇄 할 템플릿 일치 =를 입력 할 때마다 나는 현재 단지 GenreMapping의 하위 요소로 이전에 취득 (DepartmentCode, DepartmentName, Genre)!

나는 내 인생에서 내가 뭘 잘못하고 있는지 알아낼 수 없다. 어떤 helpo/suggestions도 크게 감사 할 것입니다.

는 현재 실제 XSLT 목록을 검색 할 수 있습니다 :.

<?xml version="1.0"?> 
<xsl:stylesheet version="2.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

<!-- Define the global parameters --> 
<xsl:param name="TransformationID"/> 
<xsl:param name="TransformationType"/> 

<!-- Specify that XML is the desired output type --> 
<xsl:output method="xml" encoding="UTF-8" indent="yes"/> 

<!-- Set up the genre matching capability --> 
<xsl:key name="genre-lookup" match="GenreMapping" use="@DepartmentCode"/> 

<xsl:variable name="documentPath"><xsl:value-of select="concat('GenreSet_',$TransformationID,'.xml')"/></xsl:variable> 
<xsl:variable name="lookupDoc" select="document($documentPath)"/> 

<!-- Start the first match on the Root level --> 
<xsl:template match="/something"> 
<stuff> 
    <xsl:for-each select="monkey"> 
     <Genre> 
     <xsl:apply-templates select="$lookupDoc/*"> 
     <xsl:with-param name="curr-genrecode" select="string(genrecode)"/> 
     </xsl:apply-templates> 
     </Genre> 
    </xsl:for-each> 
</stuff> 
</xsl:template > 

<xsl:template match="GetGenreMappingObjectsResponse"> 
    <xsl:param name="curr-genrecode"/> 
    <xsl:value-of select="key('genre-lookup', $curr-genrecode, $lookupDoc)/@Genre"/> 
</xsl:template> 
</xsl:stylesheet> 

TransformationID가 항상 곁에 (124)이다 (그래서 올바른 조회 파일이 열을 유형은 내가 현재 사용하되 의도하고 있지 않다 단지 이름입니다

.

하나는 key 기능의 3 매개 변수 버전입니다 세 번째 매개 변수는 당신이 (당신은 키가 작업 할 루트 노드를 지정할 수 있습니다 : XSLT 2.0

+0

어떤 XSLT 컴파일러를 사용하고 있습니까? – Welbog

+0

색슨 -B 9.1.0.7 –

+0

그림. 나는 그 것에 익숙하지 않다. 어쩌면 나중에 Tomalak이 당신을 도울 것입니다. 열쇠가없는 방법이 효과가 있습니까? – Welbog

답변

7

당신이 원하는 당신이 할 수있는 두 가지 방법이 있습니다. 기본적으로 주 문서의 항상 루트) :

<xsl:value-of select="key('genre-lookup', $curr-genrecode,$lookupDoc)/@Genre"/> 

또 다른 방법은 $lookupDoc 노드 아래에 key 기능을 사용하는 것입니다

:

<xsl:value-of select="$lookupDoc/key('genre-lookup', $curr-genrecode)/@Genre"/> 

두 방법 모두가 XSLT 2.0 specification on keys에 설명하고있다은하지 않습니다 XSLT 1.0에서 작동합니다.

XSLT 1.0으로 제한되어있는 경우에는 키를 사용하지 않기 위해이 내용을 다시 작성해야합니다.

<xsl:value-of select="$lookupDoc//GenreMapping[@DepartmentCode = $curr-genrecode]/@Genre"/> 

아하!apply-templates 호출에있는 select="$lookupDoc" 호출이 예상 한 템플릿이 아니라 기본 템플릿을 호출하므로 매개 변수가 손실됩니다. 이것에

변경을 :

<xsl:apply-templates select="$lookupDoc/*"> 
    <xsl:with-param name="curr-genrecode" select="string(genrecode)"/> 
</xsl:apply-templates> 

제대로 템플릿을 호출하고 키가 작동합니다. (

<xsl:variable name="lookupDoc" select="document('XMLFile2.xml')"/> 
    <xsl:key name="genre-lookup" match="GenreMapping" use="@DepartmentCode"/> 
    <xsl:template match="/something"> 
    <stuff> 
     <xsl:for-each select="monkey"> 
     <Genre> 
      <xsl:apply-templates select="$lookupDoc/*"> 
      <xsl:with-param name="curr-genrecode" select="string(genrecode)"/> 
      </xsl:apply-templates> 
     </Genre> 
     </xsl:for-each> 
    </stuff> 
    </xsl:template> 
    <xsl:template match="GetGenreMappingObjectsResponse"> 
    <xsl:param name="curr-genrecode"/> 
    <xsl:value-of select="key('genre-lookup',$curr-genrecode,$lookupDoc)/@Genre"/> 
    </xsl:template> 

(Source of information)

+0

안녕하세요, 감사합니다. 나는이 두 가지 방법 (마스터 템플릿 내에서이 양식을 사용하여 apply-template을 호출하지 않음)을 이미 시도했다가 다시 시도했다. 나는 아직도 비어있는 요소를 얻는다?! 키 설정시 잘못되었을 수도 있습니다. 안부 –

+0

이것은 XSLT 컴파일러의 특정 문제 일 수 있지만'$ lookupDoc/*'를 사용하면 기반을 다뤄야합니다. – Welbog

+0

안녕하세요, 업데이트 된 코드를 사용하여 더 이상 빈 요소를 얻지 않아 공백으로 가득 찼습니다 (24 행). 나는 정말 당황 스럽다. –

0

OK, 그래서 이것은 약간 정신이며 나는 그것을 이해하는 주장하지 않습니다하지만 그것은 작동합니다

그래서 최종 XSLT 시트는 다음과 같이 보일 것입니다 소프트웨어 분야에서의 커리어와 같음).

내가 겪고있는 문제는 apply-templates를 호출하고 외부 문서를 변수로 전달할 때 "Genremapping"이라고 불리는 템플릿조차도 일치하지 않는다는 것입니다.

그래서 나는 그것을 잡기 위해 와일드 카드를 사용했다. apply-templates를 호출 할 때 나는 관심있는 자식으로 노드 집합을 좁혔다. 이것은 노드의 이름을 출력 할 수있는 매우 중대한 역할을했다. "GenreMapping"을 참조하십시오. 그러나 "GenreMapping"이라고 불렀던 어떤 템플릿에도 들어 가지 않았습니다.

이것은 모든 단일 GenreMapping 노드에 대해 내 새 템플릿 일치가 호출된다는 것을 의미하므로 약간 비효율적 일 수 있습니다. 내가 그때 깨달은 것은 술어가 매치되면 인쇄물을 출력하는 것이 전부였다. 에 일치 없기 때문에 주, 마지막 key_value 올바르게 코드 항목이없는 : 출력

... 
<xsl:template match="/something"> 
<stuff> 
    <xsl:for-each select="monkey"> 
     <Genre> 
     <key_value><xsl:value-of select="genrecode"/></key_value> 
     <xsl:variable name="key_val"><xsl:value-of select="genrecode"/></xsl:variable> 
     <code> 
     <xsl:apply-templates select="$lookupDoc/*/*/*/*"> 
     <xsl:with-param name="curr-genrecode" select="string(genrecode)"/> 
     </xsl:apply-templates> 

     </code> 
     </Genre> 
    </xsl:for-each> 
</stuff> 
</xsl:template > 


<xsl:template match="*"> 
    <xsl:param name="curr-genrecode"/> 
    <xsl:value-of select=".[@DepartmentCode = $curr-genrecode]/@Genre"/> 
</xsl:template> 
... 

모두 :

는 그래서이 지금 (전혀로 사용하는 키)처럼 보인다 조회 문서.

<?xml version="1.0" encoding="UTF-8"?> 
<stuff xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <Genre> 
     <key_value>AAA</key_value> 
     <code>10 - NEWS</code> 
    </Genre> 
    <Genre> 
     <key_value>AAA</key_value> 
     <code>10 - NEWS</code> 
    </Genre> 
    <Genre> 
     <key_value>BBB</key_value> 
     <code>11 - NEWS</code> 
    </Genre> 
    <Genre> 
     <key_value>SVVS</key_value> 
     <code/> 
    </Genre> 
</stuff> 

우편 번호로 답변하십시오. Welbog에게 도움을 주셔서 감사합니다.

+0

이상한 솔루션 임에도 불구하고 실용적인 해결책을 찾은 것을 기쁘게 생각합니다. – Welbog