0

私はアマチュアで、最近 XML と XSLT を始めました。以下の XML から XSLT ファイルを作成するように依頼されました。

 <?xml version="1.0" encoding="UTF-8" ?>
 <event>
 <title>Test 1</title>
 <description>The first test</description>
 <location>
<postalcode>A1A 1A1</postalcode>
<city>Vancouver</city>
<province>BC</province>
<streetaddress>Arina street east</streetaddress>
 </location>
 <attendees>
<name>John</name>
<email>example@gmail.com</email>
<phone>778777777</phone>
 </attendees>
 </event>

このXSLTファイルを作りました

<?xml version="1.0" encoding="utf-8"?>
<!-- event.xsl -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
 <html>
   <head><title><xsl:value-of select="title"/></title></head>
 <body>
 <xsl:apply-templates />
 </body>
 </html>
 </xsl:template>

 <xsl:template match="event">
 <h2><xsl:value-of select="title"/></h2>
 <xsl:apply-templates />
 </xsl:template>

 <xsl:template match="description">
 <p><xsl:value-of select="description"/></p>
 <xsl:apply-templates />
 </xsl:template>

 <xsl:template match="location">
 <p>
<xsl:value-of select="streetaddress"/>
<xsl:value-of select="city"/>
<xsl:value-of select="province"/>
<xsl:value-of select="postalcode"/> 
</p>
<xsl:apply-templates />
</xsl:template>

<xsl:template match="attendees">
<xsl:for-each select="event/attendees">
<p>
<xsl:value-of select="name"/>
<xsl:value-of select="email"/>
<xsl:value-of select="phone"/>
</p>
</xsl:for-each>
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>

これは生成された HTML です

 <html>
  <head>
  <META http-equiv="Content-Type" content="text/html; charset=UTF-16">
   <title></title></head>
  <body>
   <h2>Test 1</h2>
     Test 1
   <p></p>The first test
   <p>Arina street eastVancouverBCA1A 1A1</p>
   A1A 1A1
   Vancouver
   BC
   Arina street east

John
example@gmail.com
778777777
 </body>
 </html>

これは私が探している目的のhtmlです

<html>
<head>
<title>Test 1</title>
<body>
<h2>Test 1</h2>
<p>The first test</p>
<p>
Ariana Street East<br>
Vancouver<br>
BC , A1A 1A1<br>
</p>
<!-- repeat-->
<p>
Name:john<br>
Email:example@gmail.com<br>
Phone:77877777
</p>

<p>
Name:john2<br>
Email:example2@gmail.com<br>
Phone:77877778
</p>
</body>
</html>

HTMLファイルを作成すると、めちゃくちゃになります。私の間違いがどこにあるのか教えていただけますか?簡単に説明された記事はありますか?ありがとうございました

4

2 に答える 2

1

このわずかに適応したバージョンを試すことができます:

<?xml version="1.0" encoding="utf-8"?>
<!-- event.xsl -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html>
            <head>
                <title>
                    <xsl:value-of select="event/title"/>
                </title>
            </head>
            <body>
                <xsl:apply-templates />
            </body>
        </html>
    </xsl:template>

    <xsl:template match="title" />

    <xsl:template match="event">
        <h2>
            <xsl:value-of select="title"/>
        </h2>
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="description">
        <p>
            <xsl:value-of select="description"/>
        </p>
    </xsl:template>

    <xsl:template match="location">
        <p>
            <xsl:value-of select="streetaddress"/>
            <br/>
            <xsl:value-of select="city"/>
            <br/>
            <xsl:value-of select="province"/>
            <br/>
            <xsl:value-of select="postalcode"/>
            <br/>
        </p>
    </xsl:template>

    <xsl:template match="attendees">
        <p>
            Name: <xsl:value-of select="name"/><br/>
            Email: <xsl:value-of select="email"/><br/>
            Phone: <xsl:value-of select="phone"/><br/>
        </p>
    </xsl:template>
</xsl:stylesheet>

次の出力が生成されます。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test 1</title>
</head>
<body>
<h2>Test 1</h2>

        <p></p>
        <p>Arina street east<br>Vancouver<br>BC<br>A1A 1A1<br></p>
        <p>
                        Name: John<br>
                        Email: example@gmail.com<br>
                        Phone: 778777777<br></p>
</body>
</html>
于 2013-06-13T11:04:13.133 に答える
1

「attendess」パターンマッチにエラーがあると思います:

<xsl:template match="attendees">
    <xsl:for-each select="event/attendees"> 
    <p>...

「for-each」命令は冗長です。出席者テンプレートは、イベント テンプレートの「apply-templates」命令から適用されます。

最初のタイトル セクションが機能しません。次のように更新します。

<title><xsl:value-of select="event/title"/></title>

出席者と場所の行頭にフィールド名を追加
し、行末に要素を追加します

<xsl:template match="location">
    <p>
        address:<xsl:value-of select="streetaddress"/><br> 
        city:<xsl:value-of select="city"/><br> 
        province:<xsl:value-of select="province"/><br> 
        postalcode:<xsl:value-of select="postalcode"/> <br> 
    </p>
    <xsl:apply-templates />
</xsl:template>
于 2013-06-13T06:47:11.043 に答える