2016-11-18 5 views
0

誰かがXMLノードを1つのhtml出力要素に参加させてくれることを願っています。XSLTテンプレートは、1つのhtml要素に複数のノードを一致させて出力します

私はこのようなXMLがあります。

<book> 
    <section type="chapter"> 
    <p type="chapterNumber">Chapter 1</p> 
    <p type="chapterTitle">This is the very first chapter of this book</p> 
    <p type="normaltext">1 Lorem ipsum dolor sit amet</p> 
    </section> 

    <section type="chapter"> 
    <p type="chapterNumber">Chapter 2</p> 
    <p type="chapterTitle">This is the second chapter of this book</p> 
    <p type="normaltext">2 Lorem ipsum dolor sit amet</p> 
    </section> 

    <section type="chapter"> 
    <p type="chapterNumber">Chapter 3</p> 
    <p type="chapterTitle">This is the third chapter of this book</p> 
    <p type="normaltext">3 Lorem ipsum dolor sit amet</p> 
    </section> 
</book> 

私はこのXMLをHTMLに変換したい(各セクションのための1つのH1タグにp型=「chapterNumber」とp型=「CHAPTERTITLE」に参加をブック):

<html> 
<head><title>My book</title></head> 
    <body> 
    <section class="chapter"> 
    <h1>Chapter 1 - This is the very first chapter of this book</h1> 
    <p class="normaltext">Lorem ipsum dolor sit amet</p> 
    </section> 

    <section class="chapter"> 
    <h1>Chapter 2 - This is the second chapter of this book</h1> 
    <p class="normaltext">Lorem ipsum dolor sit amet</p> 
    </section> 

    <section class="chapter"> 
    <h1>Chapter 2 - This is the third chapter of this book</h1> 
    <p class="normaltext">Lorem ipsum dolor sit amet</p> 
    </section> 

</body> 
</html> 

これはchapterNumerは1つのH1に変換され、CHAPTERTITLEが別のH1要素に変換されて今の私が持っているXSLT、次のとおりです。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html"/> 
<xsl:output indent="yes"/> 

<xsl:template match="/"> 
    <html> 
     <head><title>My book</title></head> 
    <body> 
     <xsl:apply-templates/> 
    </body> 
    </html> 
</xsl:template> 


<xsl:template match="book/section[@type="chapter"]"> 
    <section> 
     <xsl:apply-templates/> 
    </section> 

<xsl:template match="book/section[@type="chapter"]/p[@type="chapterNumber"]"> 
    <h1> 
     <xsl:apply-templates/> 
    </h1> 
</xsl:template> 
<xsl:template match="book/section[@type="chapter"]/p[@type="chapterTitle"]"> 
    <h1> 
     <xsl:apply-templates/> 
    </h1> 
</xsl:template> 
<xsl:template match="book/section[@type="chapter"]/p[@type="normal"]"> 
    <p> 
     <xsl:apply-templates/> 
    </p> 
</xsl:template> 
</xsl:stylesheet> 
常に `p型`の最初の子要素として=「chapterNumberが」 `があるよう
+0

にコードを適応させることができ、入力規則的ですセクションタイプ= "chapter"と第2子エレメントとして 'p type =" chapterTitle "'? –

答えて

0

あなたがp type="chapterNumber"の次の兄弟を処理する場合は、直接あなたが

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:output method="html"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/"> 
    <html> 
     <head><title>My book</title></head> 
    <body> 
     <xsl:apply-templates/> 
    </body> 
    </html> 
</xsl:template> 


<xsl:template match="book/section[@type='chapter']"> 
    <section class="chapter"> 
     <xsl:apply-templates/> 
    </section> 
</xsl:template> 

<xsl:template match="book/section[@type='chapter']/p[@type='chapterNumber']"> 
    <h1> 
     <xsl:apply-templates/> 
     <xsl:text> - </xsl:text> 
     <xsl:apply-templates select="following-sibling::p[@type = 'chapterTitle']/node()"/> 
    </h1> 
</xsl:template> 

<xsl:template match="book/section[@type='chapter']/p[@type='chapterTitle']"/> 

<xsl:template match="book/section[@type='chapter']/p[@type='normaltext']"> 
    <p> 
     <xsl:apply-templates/> 
    </p> 
</xsl:template> 

</xsl:stylesheet> 
+0

Martinさん、ありがとうございました! – Thomas

関連する問題