开发者

Select DIV tag with XSLT?

I'm trying to create a new XHTML document from another XHTML document. I only want to use some of the DIV tags in the old XHTML document, but I'm not sure if I'm doing it right. To start, if I want to select a special DIV tag with ID = mbContent, could I use

<xsl:template match="x:div[@id='mbContent']">

This DIV tag contains other DIVs and content like images 开发者_JS百科and so on. How do I do if I want to use the same CSS style that is applied to the content? Is there a way to copy the CSS style or do I have to add new CSS style and how do I do that? Since the new XHTML document is going to be part of antother XHTML, I cant use HEAD tag and put a reference to the CSS stylesheet that way.

Hmm, but if I use the CSS stylesheet that is going to be in the HEAD of the main XHTML documnet, perhaps I could apply that CSS styles to this DIV, or? How do I apply styles in the new XHTML document?

I'm a little bit confused, but I hope my question isn't to confusing?! :)

Hi! I need some new help since the code below isn't working for me. It's this that isn't working xmlns:x="http://www.w3.org/1999/xhtml" and "x:div[@id='mbContent']" I think it's because I'm using a CMS tool that has a proxy module that not accept this code for some strange reason. Therefore I'm looking for some alternative solution to add CLASS or ID and also add values to DIV elements by using this instead xsl:apply-templates select="//*[@id='mbSubMenu']" and also use copy as in the example below? Preciate some new help! Thanks! :)


The Xpath used in the expression is fine until you are using 'x' as xmlns in the XSLT document.

The template will match for the <div> provided its id is mbContent and the selected context will have all the descendents.

You can change the inline CSS for the elements. Since you said that this part is going to be within some other XHTML document. You can choose XML as output. Change the inline CSS if you want to. You can also assign them different classes so that it takes global styles automatically.

The idea is that given a XML document you are transforming it to another XML document. Therefore, you can apply styles as you like it.

I hope that answers your question.

P.S. use proper xmlns in the XPath expression.

Let's assume following is the HTML doc.

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <title></title>
</head>
<body>
    <div id="mbContent">
        <div>
            <span>Some complex structure</span>
        </div>    
    </div>
</body>

Apply the following XSL

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:x="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="x">

<xsl:output method="xml" indent="yes"/>

<xsl:strip-space elements="*"/>

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
</xsl:template>

<xsl:template match="x:div[@id='mbContent']">
    <xsl:copy>
        <xsl:attribute name="class">
            <xsl:text>someNewStyle</xsl:text>
        </xsl:attribute>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

This will result in the following output.

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <title/>
</head>
<body>
    <div class="someNewStyle" id="mbContent">
        <div>
            <span>Some complex structure</span>
        </div>
    </div>
</body>

You can change the XSL to suit your need.

Regards,

Ravish.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜