2013-06-25 3 views
1

아래 구조와 같은 XML이 있습니다.모든 요소의 concat 값

<?xml version="1.0" encoding="ISO-8859-1"?> 

<bookstore> 
    <book> 
      <title lang="eng">Harry Potter</title> 
      <price>29.99</price> 
    </book> 

    <book> 
      <title lang="eng">Learning XML</title> 
      <price>39.95</price> 
    </book> 
</bookstore> 

나는 <xsl:variable name="titles" select="/bookstore/book/title"/>으로 모든 title 노드를 추출했다. 자,이 제목들을 작은 따옴표로 묶고 쉼표로 구분하여 변수에 저장하여 결과가 다음과 같이 보이도록하십시오 : 'Harry Potter','Learning XML'. 어떻게해야합니까?

답변

1

알려진 목록 수 concat()에 의해 "조립"할 수 있습니다. 그러나 귀하의 경우는, (titels에서) 목록에 속하는 항목 수있는 유일한 가능성 모르는 XLST을-1.0 요소 (for-each 또는 apply-templates 그들을 CONCAT을 반복하는 것입니다

이을 시도해보십시오.

<xsl:variable name="titles" select="/bookstore/book/title"/> 
    <xsl:variable name="titles_str" > 
     <xsl:for-each select="$titles" > 
      <xsl:if test="position() > 1 ">, </xsl:if> 
      <xsl:text>'</xsl:text> 
      <xsl:value-of select="."/> 
      <xsl:text>'</xsl:text> 
     </xsl:for-each> 
    </xsl:variable> 
    <xsl:value-of select="$titles_str"/> 
0

이 하나 변수 당신의 제목을 변경해야합니다 :

<xsl:variable name="titles"> 
    <xsl:for-each select="/bookstore/book/title"> 
     <xsl:text>'</xsl:text><xsl:value-of select="."/><xsl:text>'</xsl:text> 
     <xsl:if test="position()!=last()">, </xsl:if> 
    </xsl:for-each> 
    </xsl:variable> 

가 원하는 출력을 얻을 : 값의

'Harry Potter', 'Learning XML'