2009-05-12 3 views
0

xsl : key 및 key() 함수에 대한 자습서를 찾았지만 어쨌든 여전히 이해가 부족합니다.XSLT 외부 조회 테이블 올바른 사용법 - key() 함수?

내가해야 할 XML-XML 변환이 있습니다. 여기에는 소스 XML에서 문자열 값을 가져와 적절한 룩업 테이블 (제공된)에서 적절한 숫자 코드를 찾아서이 코드를 결과 XML.

조회 테이블에 대해 xsl : for-each를 수행하는 작업 버전이 있지만 차선책 인 것으로 의심되며 select = "key ('CR-Lookup', $ CR을 사용해야했는지 알고 싶습니다.) "대신 somhow.

그럼, 내가하고 싶은 것은 (나무의 깊은 부분)이다 : 나는 이렇게 같이했습니다

<Contributor> 
<ContributorRole id="7" code="818"/> 
<Value id="Name">Anglet, J.</Value> 
</Contributor> 

파일을 :

<Contributor> 
<ContributorRole>producer</ContributorRole> 
<ContributorName>Anglet, J.</ContributorName> 
</Contributor> 

는 다음과 같이로 변환합니다 :

조회 테이블 파일 lookup_ContributorRole.xml :

<lookup id="ContributorRole"> 
<row> 
    <id>7</id> 
    <parentid>NULL</parentid> 
    <valueMember>1</valueMember> 
    <displayMember>producer</displayMember> 
    <code>818</code> 
    <externalId>NULL</externalId> 
    <description>NULL</description> 
</row> 
<!-- more <row>s...--> 
</lookup> 

AMD 내가 일치 할을 시도합니다 XSLT 파일 : 당신이 키를 사용하기 전에

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:foxml="info:fedora/fedora-system:def/foxml#" 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
    xmlns:rel="info:fedora/fedora-system:def/relations-external#" 
    xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" 
    xmlns:dc="http://purl.org/dc/elements/1.1/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema" 
    xmlns:audit="info:fedora/fedora-system:def/audit#" 
    xmlns:fedoraxsi="http://www.w3.org/2001/XMLSchema-instance" 

    exclude-result-prefixes="xsl foxml rdf rel oai_dc dc xsi audit fedoraxsi" 
> 
<xsl:output omit-xml-declaration="yes" indent="yes" method="xml" /> 

<xsl:key name="CR-lookup" match="row" use="displayMember"/> 
<xsl:variable name="CRTable" select="document('lookup_ContributorRole.xml')/lookup/row"/> 

<xsl:template match="Contributor"> 
    <Contributor> 
    <xsl:variable name="CR"><xsl:value-of select="ContributorRole"/></xsl:variable> 
    <ContributorRole> 
    <xsl:for-each select="$CRTable"> 
     <xsl:if test="displayMember=$CR"> 
      <xsl:attribute name="id"><xsl:value-of select="id"/></xsl:attribute> 
      <xsl:attribute name="code"><xsl:value-of select="code"/></xsl:attribute> 
     </xsl:if> 
    </xsl:for-each> 
    </ContributorRole> 
    <Value id="Name"><xsl:value-of select="ContributorName"/></Value> 
    </Contributor> 
</xsl:template> 

    <xsl:template match="/"> 
    <DigitalObject> 
     <Core> 
      <xsl:for-each select="/foxml:digitalObject/foxml:datastream[@ID='DigitalObjectLL']/foxml:datastreamVersion"> 
      <xsl:sort select="@CREATED" order="descending"/> 
      <xsl:if test="position() = 1"> 
       <xsl:for-each select="./foxml:xmlContent/lnbdo"> 
        <xsl:apply-templates select="Contributor"/> 
       </xsl:for-each> 
      </xsl:if> 
      </xsl:for-each> 
     </Core> 
    </DigitalObject> 
    </xsl:template> 
</xsl:stylesheet> 

답변

4

당신은 상황에 맞는 문서를 전환해야을 : XSLT 2.0

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

<xsl:template match="Contributor"> 
    <Contributor> 
    <xsl:variable name="CR" select="ContributorRole"/> 
    <ContributorRole> 
     <xsl:for-each select="$CRTable"><!-- change context document --> 
     <xsl:for-each select="key('CR-lookup', $CR)"> 
      <xsl:attribute name="id"><xsl:value-of select="id"/></xsl:attribute> 
      <xsl:attribute name="code"><xsl:value-of select="code"/></xsl:attribute> 
      ... 

당신 할 수있다

<xsl:for-each select="key('CR-lookup', $CR, $CRTable)"> 
+0

아하, 고마워. 하지만 여전히 첫 번째 $ CRTable for-each를 사용해야한다는 것을 의미하므로 두 번째 for-each (select = "key (...)")와 xsl : 대신 (그것은 내가 한 짓이다). – Gnudiff

+0

나는이 코드를 오해했다고 생각합니다. 난 열쇠와 함께 xsl : if가 필요 없다는 것을 반영하도록 대답을 업데이트했다. – jelovirt

+0

아, 지금 고맙습니다! – Gnudiff

-1

나는 당신이 th를 사용하는 가장 좋은 방법을 알 필요가 있다고 생각한다. e Xslt key function.

관련 문제