2010-04-23 6 views
0

xsl : when이 만족 될 때마다 <a> 및 > 태그를 추가하고 싶습니다. 그러나 태그 내부에 채워 져야하는 데이터는 한 번만 있어야합니다. 결국 예상 결과를 보여주었습니다. XSL을 수정하여 xml 형식을 변경하는 방법

<xsl:param name="insert-file" as="document-node()" /> 
<xsl:template match="*"> 
<xsl:variable name="input">My text</xsl:variable> 
<xsl:variable name="Myxml" as="element()*"> 
    <xsl:call-template name="populateTag"> 
      <xsl:with-param name="nodeValue" select="$input"/> 
    </xsl:call-template> 
</xsl:variable> 
<xsl:copy-of select="$Myxml"></xsl:copy-of> 
</xsl:template> 

<xsl:template name="populateTag"> 
    <xsl:param name="nodeValue"/> 
    <xsl:for-each select="$insert-file/insert-data/data"> 
     <xsl:choose> 
      <xsl:when test="@index = 1"> 
       <a><xsl:value-of select="$nodeValue"></xsl:value-of></a> 
      </xsl:when>    
     </xsl:choose> 
    </xsl:for-each> 
</xsl:template>  

전류 출력 :

<?xml version="1.0" encoding="UTF-8"?> <a> 내 텍스트 </a> <a> 내 텍스트 </a> <a> 내 텍스트 </a> <a> 내 텍스트 </a>

나는 템플릿 "populateTag"가 아래의 형식으로 xml을 반환하도록합니다. 어떻게 "populateTag"템플릿을 수정하여 동일하게 유지합니까?

예상 출력 템플릿에서 "populateTag" <?xml version="1.0" encoding="UTF-8"?> <a><a><a><a> 내 텍스트 </a></a></a></a>

아이디어를주십시오.

답변

1

그런 일이 일어나려면 일종의 재귀가 필요합니다 (a 요소를 중첩하는 데 필요함). 당신의 응답을

<xsl:param name="insert-file" as="document-node()" /> 
<xsl:template match="*"> 
<xsl:variable name="input">My text</xsl:variable> 
<xsl:variable name="Myxml" as="element()*"> 
    <xsl:call-template name="populateTag"> 
      <xsl:with-param name="nodeValue" select="$input"/> 
      <xsl:with-param name="position" select="1"/> 
    </xsl:call-template> 
</xsl:variable> 
<xsl:copy-of select="$Myxml"></xsl:copy-of> 
</xsl:template> 

<xsl:template name="populateTag"> 
    <xsl:param name="nodeValue"/> 
    <xsl:param name="position"/> 
    <xsl:variable name="total" select="count($insert-file/insert-data/data[@index = 1])" /> 
    <xsl:for-each select="$insert-file/insert-data/data[@index = 1]"> 
     <xsl:if test="position() = $position" > 
      <xsl:choose> 
       <xsl:when test="position() = $total"> 
        <a><xsl:value-of select="$nodeValue"></xsl:value-of></a> 
       </xsl:when>    
       <xsl:otherwise> 
       <a>  
         <xsl:call-template name="populateTag"> 
           <xsl:with-param name="nodeValue" select="$input"/> 
           <xsl:with-param name="position" select="$position+1"/> 
         </xsl:call-template> 
       </a> 
       </xsl:otherwise> 
      </xsl:choose> 
     </xsl:if> 
    </xsl:for-each> 
</xsl:template> 
+0

감사 : 나는 샘플 XML 문서를 가지고 있지 않기 때문에 시도하지 않고

. 그것은 작동합니다. – user323719

관련 문제