簡體   English   中英

在XML中使用CSS和XSL

[英]Using CSS and XSL in XML

我有一個項目,我需要使用XSLT將xml轉換為html並為其添加一個CSS。 我似乎無法讓CSS影響文檔,任何幫助都將非常有用!

CSS只是居中對齊,因為我可以很容易地看到它是否在工作,一旦知道它在工作,就可以對其進行編輯,這就是我想要的樣子。 但是基本上XSL可以正常工作,但是我無法獲得CSS影響xml

XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="BookDescription.xsl" type="text/xsl"?>
<?xml-stylesheet type="text/css" href="book_details.css"?>
<!--ORACLE BOOK-->
<Book>
    <bookID>0-07-882122-3</bookID>
    <bookTitle>Oracle: A Beginner's Guide</bookTitle>
    <bookCategory>database</bookCategory>
    <bookDescription>A beginner's guide to the complex and powerful Oracle database management system. Teaches you how to set up, query and manage your database, including principles of database design, how to manage data, generate reports and tune the system for optimal performance.</bookDescription>
    <bookPrice>30.00</bookPrice>
    <bookAuthor>Michael Abbey</bookAuthor>
    <bookImage>images/Oracle.JPG</bookImage>
    <bookInfo>Oracle.xml</bookInfo>
</Book>

XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="Book">
        <h3>
            <xsl:value-of select="bookTitle"/>
        </h3>
        <img alt="">
            <xsl:attribute name="src"><xsl:value-of select="bookImage"/></xsl:attribute>
        </img>
        <div>
            <xsl:text>ISBN: </xsl:text>
            <xsl:value-of select="bookID"/>
        </div>
        <div>
            <xsl:text>Author: </xsl:text>
            <xsl:value-of select="bookAuthor"/>
        </div>
        <div>
            <xsl:text>Category: </xsl:text>
            <xsl:value-of select="bookCategory"/>
        </div>
        <div>
            <xsl:text>Description: </xsl:text>
            <xsl:value-of select="bookDescription"/>
        </div>
        <div>
            <xsl:text>Price: </xsl:text>
            <xsl:value-of select="bookPrice"/>
        </div>

    </xsl:template>

    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates select="Book"/>
            </body>
        </html> 
    </xsl:template>
</xsl:stylesheet>

的CSS

div{
    text-align: center;
}

bookID{
    text-align: center;
}

您必須將link添加到CSS文件:

<xsl:template match="/">
    <html>
        <head>
            <link rel="stylesheet" href="path/to/your/css/file.css">
        </head>
        <body>
            <xsl:apply-templates select="Book"/>
        </body>
    </html> 
</xsl:template>

要么:

<xsl:template match="/">
    <html>
        <head>
            <style>
                /* Your CSS here */ 
            </style>
        </head>
        <body>
            <xsl:apply-templates select="Book"/>
        </body>
    </html> 
</xsl:template>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM