2

呼び出しテンプレートの代わりに適用テンプレートを使用するように mny スタイルシートを変換しましたが、より複雑な他のスタイルシートでは問題なく機能しましたが、これははるかに単純なテンプレートであっても機能しないようです。

出力されるのは、sex ノードと userlevel ノードだけです。それは私のXpathに関係していると思います。

私が望むのは、 < user > 情報を出力することだけです。他には何もありません

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template name="hoo" match="/">
        <html>
            <head>
                <title>Registered Members</title>
                <link rel="stylesheet" type="text/css" href="user.css" />
            </head>
            <body>
                <h1>Registered Members</h1>
                <xsl:for-each select="folktask/member/user">

                    <div class="userdiv">

                        <xsl:apply-templates/>

                    </div>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="folktask/member/user">
        <xsl:apply-templates select="@id"/>
        <xsl:apply-templates select="personal/name"/>
        <xsl:apply-templates select="personal/address1"/>
        <xsl:apply-templates select="personal/city"/>
        <xsl:apply-templates select="personal/county"/>
        <xsl:apply-templates select="personal/postcode"/>
        <xsl:apply-templates select="personal/telephone"/>
        <xsl:apply-templates select="personal/mobile"/>
        <xsl:apply-templates select="personal/email"/>
        <xsl:apply-templates select="personal"/>
        <xsl:apply-templates select="account/username"/>
        <xsl:apply-templates select="account"/>
    </xsl:template>

    <xsl:template match="@id">
        <div class="heading bold"><h2>USER ID: <xsl:value-of select="." /></h2></div>
    </xsl:template>

    <xsl:template match="personal/name">
        <div class="small bold">NAME:</div> 
        <div class="large"><xsl:value-of select="." /></div>
    </xsl:template>

    <xsl:template match="personal/address1">
        <div class="small bold">ADDRESS:</div>
        <div class="large"><xsl:value-of select="." /></div>
    </xsl:template>

    <xsl:template match="personal/city">
        <div class="small bold">CITY:</div>
        <div class="large"><xsl:value-of select="." /></div>
    </xsl:template>

    <xsl:template match="personal/county">
        <div class="small bold">COUNTY:</div>
        <div class="large"><xsl:value-of select="." /></div>
    </xsl:template>

    <xsl:template match="personal/postcode">
        <div class="small bold">POSTCODE:</div>
        <div class="large"><xsl:value-of select="." /></div>
    </xsl:template>

    <xsl:template match="personal/telephone">
        <div class="small bold">TELEPHONE:</div>
        <div class="large"><xsl:value-of select="." /></div>
    </xsl:template>

    <xsl:template match="personal/mobile">
        <div class="small bold">MOBILE:</div>
        <div class="large"><xsl:value-of select="." />
        </div>
    </xsl:template>

    <xsl:template match="personal/email">
        <div class="small bold">EMAIL:</div>
        <div class="large">
            <xsl:element name="a">
                <xsl:attribute name="href">
                    <xsl:text>mailto:</xsl:text>
                    <xsl:value-of select="." />
                </xsl:attribute>
                <xsl:value-of select="." />
            </xsl:element>
        </div>
    </xsl:template>

    <xsl:template match="personal">
    <div class="small bold">SEX:</div>
    <div class="colored bold">
        <xsl:choose>
            <xsl:when test="sex='Male'">
                <div class="sex male"><xsl:value-of select="sex/."/></div>
            </xsl:when>
            <xsl:otherwise>
                <div class="sex female"><xsl:value-of select="sex/."/></div>
            </xsl:otherwise>
        </xsl:choose>
    </div>
    </xsl:template>

    <xsl:template match="account/username">
        <div class="small bold">USERNAME:</div>
        <div class="large"><xsl:value-of select="." /></div>
    </xsl:template>


    <xsl:template match="account">
        <div class="small bold">ACCOUNT TYPE:</div>
        <div class="colored ">
        <xsl:choose>
            <xsl:when test="userlevel='1'">
                <div class="nml bold">Normal User</div>
            </xsl:when>

            <xsl:when test="userlevel='2'">
                <div class="vol bold">Volunteer</div>
            </xsl:when>

            <xsl:when test="userlevel='3'">
                <div class="org bold">Organiser</div>
            </xsl:when>

            <xsl:otherwise>
                <div class="name adm bold">Administrator</div>
            </xsl:otherwise>
        </xsl:choose>
        </div>
    </xsl:template>

</xsl:stylesheet> 

そして私のxmlのいくつか

<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="users.xsl"?>
<folktask xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="folktask.xsd">
    <member>
        <user id="1">
            <personal>
                <name>Abbie Hunt</name>
                <sex>Female</sex>
                <address1>108 Access Road</address1>
                <address2></address2>
                <city>Wells</city>
                <county>Somerset</county>
                <postcode>BA5 8GH</postcode>
                <telephone>01528927616</telephone>
                <mobile>07085252492</mobile>
                <email>adrock@gmail.com</email>
            </personal>
            <account>
                <username>AdRock</username>
                <password>269eb625e2f0cf6fae9a29434c12a89f</password>
                <userlevel>4</userlevel>
                <signupdate>2010-03-26T09:23:50</signupdate>
            </account>
        </user>
        <volunteer id="1">
            <roles></roles>
            <region>South West</region>
        </volunteer>
    </member>
    <member>
        <user id="2">
            <personal>
                <name>Aidan Harris</name>
                <sex>Male</sex>
                <address1>103 Aiken Street</address1>
                <address2></address2>
                <city>Chichester</city>
                <county>Sussex</county>
                <postcode>PO19 4DS</postcode>
                <telephone>01905149894</telephone>
                <mobile>07784467941</mobile>
                <email>ambientexpert@yahoo.co.uk</email>
            </personal>
            <account>
                <username>AmbientExpert</username>
                <password>8e64214160e9dd14ae2a6d9f700004a6</password>
                <userlevel>2</userlevel>
                <signupdate>2010-03-26T09:23:50</signupdate>
            </account>
        </user>
        <volunteer id="2">
            <roles>Van Driver,gas Fitter</roles>
            <region>South Central</region>
        </volunteer>
    </member>
</folktask>
4

2 に答える 2

3

問題はこのコードにあります:

   <xsl:for-each select="folktask/member/user">

        <div class="userdiv">

            <xsl:apply-templates/>

        </div>
    </xsl:for-each>

userこれにより、要素ではなく、現在の要素のすべての子にテンプレートが適用されますuser

その結果、テンプレート マッチングuserは選択されません。

の子userpersonalaccountです。

これらの要素に一致するテンプレートが選択され、出力が生成されます。

解決策: かなりのクリーンアップが必要ですが、最初の明白なステップは、上記のコードを次のものに置き換えることです。

        <xsl:apply-templates select="folktask/member/user"/>

divまた、 の削除された本文から<xsl:for-each>テンプレート マッチングに移動する必要がありますfolktask/member/user

修正された XSLT コードは次のとおりです。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template name="hoo" match="/">
        <html>
            <head>
                <title>Registered Members</title>
                <link rel="stylesheet" type="text/css" href="user.css" />
            </head>
            <body>
                <h1>Registered Members</h1>
                <xsl:apply-templates select="folktask/member/user"/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="folktask/member/user">
        <div class="userdiv">
            <xsl:apply-templates select="@id"/>
            <xsl:apply-templates select="personal/name"/>
            <xsl:apply-templates select="personal/address1"/>
            <xsl:apply-templates select="personal/city"/>
            <xsl:apply-templates select="personal/county"/>
            <xsl:apply-templates select="personal/postcode"/>
            <xsl:apply-templates select="personal/telephone"/>
            <xsl:apply-templates select="personal/mobile"/>
            <xsl:apply-templates select="personal/email"/>
            <xsl:apply-templates select="personal"/>
            <xsl:apply-templates select="account/username"/>
            <xsl:apply-templates select="account"/>
        </div>
    </xsl:template>

    <xsl:template match="@id">
        <div class="heading bold"><h2>USER ID: <xsl:value-of select="." /></h2></div>
    </xsl:template>

    <xsl:template match="personal/name">
        <div class="small bold">NAME:</div>
        <div class="large"><xsl:value-of select="." /></div>
    </xsl:template>

    <xsl:template match="personal/address1">
        <div class="small bold">ADDRESS:</div>
        <div class="large"><xsl:value-of select="." /></div>
    </xsl:template>

    <xsl:template match="personal/city">
        <div class="small bold">CITY:</div>
        <div class="large"><xsl:value-of select="." /></div>
    </xsl:template>

    <xsl:template match="personal/county">
        <div class="small bold">COUNTY:</div>
        <div class="large"><xsl:value-of select="." /></div>
    </xsl:template>

    <xsl:template match="personal/postcode">
        <div class="small bold">POSTCODE:</div>
        <div class="large"><xsl:value-of select="." /></div>
    </xsl:template>

    <xsl:template match="personal/telephone">
        <div class="small bold">TELEPHONE:</div>
        <div class="large"><xsl:value-of select="." /></div>
    </xsl:template>

    <xsl:template match="personal/mobile">
        <div class="small bold">MOBILE:</div>
        <div class="large"><xsl:value-of select="." />
        </div>
    </xsl:template>

    <xsl:template match="personal/email">
        <div class="small bold">EMAIL:</div>
        <div class="large">
            <xsl:element name="a">
                <xsl:attribute name="href">
                    <xsl:text>mailto:</xsl:text>
                    <xsl:value-of select="." />
                </xsl:attribute>
                <xsl:value-of select="." />
            </xsl:element>
        </div>
    </xsl:template>

    <xsl:template match="personal">
    <div class="small bold">SEX:</div>
    <div class="colored bold">
        <xsl:choose>
            <xsl:when test="sex='Male'">
                <div class="sex male"><xsl:value-of select="sex/."/></div>
            </xsl:when>
            <xsl:otherwise>
                <div class="sex female"><xsl:value-of select="sex/."/></div>
            </xsl:otherwise>
        </xsl:choose>
    </div>
    </xsl:template>

    <xsl:template match="account/username">
        <div class="small bold">USERNAME:</div>
        <div class="large"><xsl:value-of select="." /></div>
    </xsl:template>


    <xsl:template match="account">
        <div class="small bold">ACCOUNT TYPE:</div>
        <div class="colored ">
        <xsl:choose>
            <xsl:when test="userlevel='1'">
                <div class="nml bold">Normal User</div>
            </xsl:when>

            <xsl:when test="userlevel='2'">
                <div class="vol bold">Volunteer</div>
            </xsl:when>

            <xsl:when test="userlevel='3'">
                <div class="org bold">Organiser</div>
            </xsl:when>

            <xsl:otherwise>
                <div class="name adm bold">Administrator</div>
            </xsl:otherwise>
        </xsl:choose>
        </div>
    </xsl:template>

</xsl:stylesheet>

修正された変換を実行すると、意図した結果が得られるようになりました

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-16">
<title>Registered Members</title>
<link rel="stylesheet" type="text/css" href="user.css">
</head>
<body>
<h1>Registered Members</h1>
<div class="userdiv">
<div class="heading bold"><h2>USER ID: 1</h2></div>
<div class="small bold">NAME:</div>
<div class="large">Abbie Hunt</div>
<div class="small bold">ADDRESS:</div>
<div class="large">108 Access Road</div>
<div class="small bold">CITY:</div>
<div class="large">Wells</div>
<div class="small bold">COUNTY:</div>
<div class="large">Somerset</div>
<div class="small bold">POSTCODE:</div>
<div class="large">BA5 8GH</div>
<div class="small bold">TELEPHONE:</div>
<div class="large">01528927616</div>
<div class="small bold">MOBILE:</div>
<div class="large">07085252492</div>
<div class="small bold">EMAIL:</div>
<div class="large"><a href="mailto:adrock@gmail.com">adrock@gmail.com</a></div>
<div class="small bold">SEX:</div>
<div class="colored bold">
<div class="sex female">Female</div>
</div>
<div class="small bold">USERNAME:</div>
<div class="large">AdRock</div>
<div class="small bold">ACCOUNT TYPE:</div>
<div class="colored ">
<div class="name adm bold">Administrator</div>
</div>
</div>
<div class="userdiv">
<div class="heading bold"><h2>USER ID: 2</h2></div>
<div class="small bold">NAME:</div>
<div class="large">Aidan Harris</div>
<div class="small bold">ADDRESS:</div>
<div class="large">103 Aiken Street</div>
<div class="small bold">CITY:</div>
<div class="large">Chichester</div>
<div class="small bold">COUNTY:</div>
<div class="large">Sussex</div>
<div class="small bold">POSTCODE:</div>
<div class="large">PO19 4DS</div>
<div class="small bold">TELEPHONE:</div>
<div class="large">01905149894</div>
<div class="small bold">MOBILE:</div>
<div class="large">07784467941</div>
<div class="small bold">EMAIL:</div>
<div class="large"><a href="mailto:ambientexpert@yahoo.co.uk">ambientexpert@yahoo.co.uk</a></div>
<div class="small bold">SEX:</div>
<div class="colored bold">
<div class="sex male">Male</div>
</div>
<div class="small bold">USERNAME:</div>
<div class="large">AmbientExpert</div>
<div class="small bold">ACCOUNT TYPE:</div>
<div class="colored ">
<div class="vol bold">Volunteer</div>
</div>
</div>
</body>
</html>
于 2010-04-23T16:35:13.570 に答える
-2

多くのエラーがあります。主なものは次のとおりです。

  • テンプレートを属性に適用できません
  • と書く<xsl:apply-templates select="personal/name"/>と、それに合わせて書くかもしれません<xsl:template match="name">。(および他のノードの場合も)
于 2010-04-23T12:54:33.963 に答える