2016-11-28 4 views
0

인터넷에서 다른 템플릿에 변수 전달에 대한 참조가 있습니다. 나는 모든 참고 문헌을 따르려고했지만, 필자가 채워야 할 가치가 없다.다른 템플릿에 변수 값 가져 오기

<Item> 
    <Test> 
     <ID>123345677</ID> 
    </Test> 
    <DisplayID>99884534</DisplayID> 
</Item> 

내가 DisplayID 표시가 null가 아닌 경우 다른, MsgId에 요소를 채우는 ID에서 값을 얻을 필요가 :이 XML 파일이 있습니다. 내 XSLT :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:template match="ID"> 
    <xsl:variable name="IDV" select="substring(.,0,35)"/> 
    <xsl:apply-templates select="DisplayID"> 
     <xsl:with-param name="IDP" select="$IDV"/> 
    </xsl:apply-templates> 
</xsl:template> 
<xsl:template match="DisplayID"> 
    <xsl:param name="IDP"/> 
    <xsl:element name="MsgId"> 
     <xsl:choose> 
      <xsl:when test=".!='' or ./*"> 
       <xsl:value-of select="substring(.,0,35)"/> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="substring($IDP,0,35)"/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:element> 
</xsl:template> 

DisplayID 표시가 나는 DisplayID 표시의 값을 제거하면 ID에서 얻는 값은 없습니다, 그러나 노력 null가 아닌 경우 조건. 내가 제대로하고 있는지 나는 모른다.

귀하의 의견은 대단히 감사합니다.

+0

"null"로 무엇을 의미하는지 명확하지 않습니다. 요소가 전혀 존재하지 않거나 요소에 문자열 값이 없음을 의미 할 수 있습니다. –

답변

0

이 시도하십시오, 참조에 대한

데모 :이 이후 http://xsltransform.net/ejivdHb/16

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ns1="http://locomotive/bypass/docx" > 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 


<xsl:template match="Item"> 
    <xsl:element name="MsgId"> 
     <xsl:choose> 
      <xsl:when test="DisplayID !='' "> 
       <xsl:value-of select="substring(DisplayID , 0 ,35)"/> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="substring(Test/ID,0,35)"/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:element> 
</xsl:template> 

</xsl:stylesheet> 
+0

정말 고마워요. – hannah

+0

@hannah도 기꺼이 도와주세요, upvote도? :) – ScanQR

+0

@ 한나 왜 지금이 용납 할 수없는 대답입니까? xslt1.0 태그도 있습니까? – ScanQR

0

는 @TechBreak에서 경기 = "항목"템플릿은

<xsl:template match="Item"> 
    <MsgId> 
     <xsl:value-of select="substring(
      if (DisplayId != '') 
      then DisplayID 
      else Test/ID, 1 ,35)"/> 
    </MsgId> 
</xsl:template> 
으로 XSLT 2.0 교체 할 수 있습니다 태그가

(문자 수는 1부터 시작됩니다.)

관련 문제