2014-01-10 3 views
0

다른 구조 요소에 스타일 요소를 적용하는 방법을 알아 내려고하고 있습니다.XSLT 텍스트 노드 복제

<?xml version="1.0" encoding="ISO-8859-1"?> 
    <catalog> 
     <cd> 
      <title>Empire Burlesque</title> 
      <artist><bold>Bob</bold> Dylan</artist> 
     </cd> 
     <cd> 
      <title>Hide your <bold>heart</bold></title> 
      <artist>Bonnie Tyler</artist> 
     </cd> 
    </catalog> 

그리고 내 최신 XSLT 시도가이

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="bold"> 
<b><xsl:value-of select="." /></b> 
</xsl:template> 

<xsl:template match="/catalog"> 
    <html> 
    <body> 
    <h2>My CD Collection</h2> 
    <table border="1"> 
     <tr bgcolor="#9acd32"> 
     <th>Title</th> 
     <th>Artist</th> 
     </tr> 
     <xsl:for-each select="cd"> 
     <tr> 
     <td><xsl:value-of select="title" /><xsl:apply-templates/></td> 
     <td><xsl:value-of select="artist" /><xsl:apply-templates/></td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </body> 
    </html> 
</xsl:template> 


</xsl:stylesheet> 

처럼 보이지만 결과는 굵은 글씨로 원하는 부분을하는 대신 데이터를 복제 :

내 XML은 다음과 같습니다.

답변

1

여기 당신이 필요로하는 작업은 다음과 같습니다

 <xsl:for-each select="cd"> 
     <tr> 
      <td><xsl:apply-templates select="title"/></td> 
      <td><xsl:apply-templates select="artist"/></td> 
     </tr> 
     </xsl:for-each> 

기본 출력에 자신의 텍스트 노드를 복사하는 것입니다 title 또는 artist 일치하는 모든 템플릿이 없기 때문에. 당신은 스스로 할 필요가 없습니다.

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:template match="bold"> 
    <b><xsl:value-of select="." /></b> 
    </xsl:template> 

    <xsl:template match="/catalog"> 
    <html> 
     <body> 
     <h2>My CD Collection</h2> 
     <table border="1"> 
      <tr bgcolor="#9acd32"> 
      <th>Title</th> 
      <th>Artist</th> 
      </tr> 
      <xsl:apply-templates select="cd"/> 
     </table> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="cd"> 
    <tr> 
     <td><xsl:apply-templates select="title"/></td> 
     <td><xsl:apply-templates select="artist"/></td> 
    </tr> 
    </xsl:template> 

</xsl:stylesheet> 
1

여기 적은 명시 적 요소 일치를 사용하는 다른 옵션들 :

여기에 더 관용적 "푸시"처리를 사용하는 수정합니다. 많은 경우에, 이것이 더 간단하고 융통성이 있기 때문에 원하는 것입니다. 경우에 따라 이 아니므로 각 요소를 개별적으로 처리해야하기 때문입니다. YMMV.

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/catalog"> 
     <html> 
      <body> 
       <h2>My CD Collection</h2> 
       <table border="1"> 
        <tr bgcolor="#9acd32"> 
         <th>Title</th> 
         <th>Artist</th> 
        </tr> 
        <xsl:apply-templates /> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 
    <xsl:template match="cd"> 
     <tr><xsl:apply-templates /></tr> 
    </xsl:template> 
    <xsl:template match="cd/*"> 
     <td><xsl:apply-templates /></td> 
    </xsl:template> 
    <xsl:template match="bold"> 
     <b><xsl:apply-templates /></b> 
    </xsl:template> 
</xsl:stylesheet> 
0

xsl : value-of을 사용하여 한 번 처리 한 다음 xsl : apply-templates를 다시 한 번 사용합니다. 두 번 처리하지 않으려면이 중 하나를 잘라냅니다.