2014-05-12 2 views
0

XML에서 XSLT 변환을 시도하고 있습니다. 책 내용을 변형하고 싶습니다. XML 파일이 있으며,이를 XSLT로 변환해야합니다. 모든 섹션의 템플릿을 만드는 것이 좋지만 XSLT에 대한 기본 경험 만 가지고 있으므로 적용 방법을 알지 못합니다.책 스타일의 XML에서 XSLT 변환

<book> 
    <section id="1"> 
     <name>Heading First chapter</name> 
     <p><i/> some text </p> 
     <p> other text </p> 
     <subsection id="1.1"> 
      <name>Heading second chapter</name> 
       <subsubsection id="1.1.1"> 
        <name>Heading third chapter</name> 
        <p>some text</p> 
       </subsubsection> 
     </subsection> 
    </section> 
</book> 

같은 HTML은 내가 원하는입니다 :

<div> 
    <h1>1 Heading First chapter</h1> 
    <p><i>some text</i> 
    <p>other text</p> 
    <div> 
     <h2>1.1 Heading second chapter</h2> 
     <div> 
      <h3>1.1.1 Heading third chapter</h3> 
      <p>some text</p>   
     </div> 
    </div> 
</div> 

내 시도 :

<xsl:template match="/"> 
     <body> 
     <xsl:for-each select="book"> 
      <xsl:apply-templates select="."></xsl:apply-templates> 
     </xsl:for-each>  
     </body> 
    </html> 
    </xsl:template> 

<xsl:template match="p"> 
    <p> 
     <xsl:apply-templates/> 
    </p> 
    </xsl:template> 

<xsl:template match="p[i]"> 
    <p> 
     <em> 
     <xsl:value-of select="."/> 
     </em> 
    </p> 
    </xsl:template> 

<xsl:template match="section/name"> 
    <p> 
     <h2>    
      <xsl:value-of select="."/> 
     </a> 
     </h2> 
    </p> 
    </xsl:template> 

당신이 변화를 만드는 방법을 알고 있나요?

+0

당신이 그 일을하지 않았다 무엇을하려고 했습니까? – Kilazur

+0

mmy post를 업데이트했습니다. – dusan

답변

2

나는 다음 XSLT 적용 할 때 : 지정된 입력 XML

<?xml version="1.0" encoding="UTF-8"?> 
<book> 
    <section id="1"> 
     <name>Heading First chapter</name> 
     <p> 
      <i/> some text </p> 
     <p> other text </p> 
     <subsection id="1.1"> 
      <name>Heading second chapter</name> 
      <subsubsection id="1.1.1"> 
       <name>Heading third chapter</name> 
       <p>some text</p> 
      </subsubsection> 
     </subsection> 
    </section> 
</book> 

그것은 생산에

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.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="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="book"> 
     <body> 
      <xsl:apply-templates select="node()" /> 
     </body> 
    </xsl:template> 

    <xsl:template match="*[contains(local-name(), 'section')]"> 
     <div> 
      <xsl:apply-templates select="node()" /> 
     </div> 
    </xsl:template> 

    <xsl:template match="name"> 
     <xsl:element name="h{count(ancestor::*[contains(local-name(), 'section')])}"> 
      <xsl:value-of select="concat(parent::*/@id, ' ', .)" /> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

을 :

<?xml version="1.0" encoding="UTF-8"?> 
<body> 
    <div> 
     <h1>1 Heading First chapter</h1> 
     <p> 
      <i/> some text </p> 
     <p> other text </p> 
     <div> 
      <h2>1.1 Heading second chapter</h2> 
      <div> 
       <h3>1.1.1 Heading third chapter</h3> 
       <p>some text</p> 
      </div> 
     </div> 
    </div> 
</body> 
관련 문제