2017-05-01 3 views
0

를 검색 할 수 있습니다 내가 가진 여러 사람 및 위치 요소가있을 수있는 다음과 같은 XML :XSLT : 속성 값으로 작업하는 또 다른 "같은 부모"요소의 속성

<ProtectionOrderResponse> 
    <Person id="PRTPER1"> 
    <!--child elements--> 
    </Person> 
    <Location id="PRTADR1"> 
    <!--child elements--> 
    </Location> 
    <PersonLocationAssociation> 
     <PersonReference ref="PRTPER1"/> 
     <LocationReference ref="PRTADR1"/> 
    </PersonLocationAssociation> 
</ProtectionOrderResponse> 

나는 XSLT를 필요로하는 각 사람을 반복 요소를 사용하여 id 속성을 검색하고이를 PersonLocationAssociation/PersonReference의 ref 속성과 일치시킵니다. 거기에서, 나는 속성 값 "PRTADR1"을 얻고 Person 속성 값 "PRTPER1"이 주어진 xslt를 사용하여 변수에 넣고 싶습니다. 의 I가 일하고 있어요 XSLT의 단순화 된 버전입니다 :

<xsl:template match="//ProtectionOrderResponse"> 
    <!--Although I'm iterating through all the Person elements, I only want to select particular Person elements that match a specific criteria, not sure how to do this yet so I will put the logic in the template section--> 
    <xsl:for-each select="//Person"> 
    <xsl:apply-templates select="."/> 
    </xsl:for-each> 
</xsl:template> 

<xsl:template match="Person"> 
<!--Logic to only process the Person element that matches a particular criteria and get the LocationReference for that Person--> 
<xsl:variable name="PerID" select="Person/@id"/> 
    <!-- need to obtain the ref attribute value of PersonLocationAssociation/LocationReference for the PersonLocationAssociation/PersonReference[@ref=$PerID]--> 
</xsl:template> 

내가 필요로하는 심판을 얻을 수있다는 PersonLocationAssociation/PersonReference에 대한 PersonLocationAssociation/LocationReference의 값은 속성 [@ 심판 = $ PerID] . 어떻게하면 좋을까요? 감사!

답변

1

키를 사용 : 귀하의 제안에 대한

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" 
    version="2.0"> 

    <xsl:key name="ref-per" match="ProtectionOrderResponse/Person" use="@id"/> 

    <xsl:key name="ref-loc" match="PersonLocationAssociation/LocationReference" use="../PersonReference/@ref"/> 

    <xsl:template match="ProtectionOrderResponse"> 
     <xsl:apply-templates select="key('ref-per', 'PRTPER1')"/> 
    </xsl:template> 

    <xsl:template match="Person"> 
     <xsl:variable name="ref" select="key('ref-loc', @id)/@ref"/> 
     <xsl:value-of select="$ref"/> 
    </xsl:template> 
</xsl:stylesheet> 
+0

감사합니다. ID를 기반으로 특정 사람에 대한 템플릿 일치를 수행 할 수있는 방법이 있는지 알고 계십니까? 즉, 다음을 사용합니다 : 와 특정 @id를 가진 사람의 템플릿 일치에서? – ptn77

+0

나는 다음과 같은 것을 시도했다 : do do work do – ptn77

+0

새로운 질문을하면,'@s : id' 속성이 전혀 보이지 않으며'Person' 요소에 이미 매치되어있는 것처럼 왜 나는 당신을 이해하지 못합니다 템플릿을 다시 적용하고 싶습니다. –