2009-10-06 28 views
10

Tôi có một câu hỏi khá ngu ngốc. Làm cách nào để đảm bảo rằng nút nội dung hỗn hợp XML của tôi không bị lẫn lộn? Tôi có, nói, một cấu trúc XML tương tự như thế này.Nút nội dung hỗn hợp XSLT

<root> 
<book> 
    <title>Stuff</title> 
    <description> This book is <i>great</i> if you need to know about stuff. 
       I suggest <link ref="Things">this one</link> if you need to know 
       about things. </description> 
</book> 
[other books] 
</root> 

tôi cần những nội dung cuối cùng để trông giống như này

<h1>List of books</h1> 
<h2><a name="Stuff"/>Stuff</h2> 
<p> This book is <i>great</i> if you need to know about stuff. 
    I suggest <a href="#Things">this one</a> if you need to know 
    about things. </p> 

Nhưng tôi không thể trích xuất các phần của nút văn bản, tôi luôn luôn lấy toàn bộ điều. Tôi đang sử dụng trục con cháu. Bất kỳ đầu mối những gì tôi đang làm sai?

Đây là XSLT tôi:

<xsl:template match="description/*"> 
    <xsl:for-each select="following-sibling::*"> 
      <xsl:choose> 
      <xsl:when test="name(.)='link'"> 
       <a href="{@ref}"><xsl:value-of select="."/></a> 
      </xsl:when> 
      <xsl:when test="name(.)='em'"> 
       <em><xsl:value-of select="."/></em> 
      </xsl:when> 
      <xsl:otherwise><p><xsl:value-of select="."/></p></xsl:otherwise>  
     </xsl:choose> 
    </xsl:for-each> 
    </xsl:template> 

Xin lưu ý rằng XML kèm theo và kết quả html chỉ là ví dụ, tôi phải đối phó với một cấu trúc lớn hơn mà tôi không kèm theo trong, vì lợi ích của sự rõ ràng.

+0

để chia sẻ xslt của bạn? –

+0

XSLT đã được chia sẻ. –

Trả lời

10

<xsl:apply-templates> là bạn của bạn:

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

    <xsl:template match="root"> 
    <h1>List of books</h1> 
    <xsl:apply-templates /> 
    </xsl:template> 

    <!-- a <book> consists of its <title> and <description> --> 
    <xsl:template match="book"> 
    <xsl:apply-templates select="title" /> 
    <xsl:apply-templates select="description" /> 
    </xsl:template> 

    <!-- <title> is turned into a <h2> --> 
    <xsl:template match="title"> 
    <h2> 
     <a name="{.}"/> 
     <xsl:value-of select="." /> 
    </h2> 
    </xsl:template> 

    <!-- <description> is turned into a <p> --> 
    <xsl:template match="description"> 
    <p> 
     <xsl:apply-templates /> 
    </p> 
    </xsl:template> 

    <!-- default rule: copy any node beneath <description> --> 
    <xsl:template match="description//*"> 
    <xsl:copy> 
     <xsl:copy-of select="@*" /> 
     <xsl:apply-templates /> 
    </xsl:copy> 
    </xsl:template> 

    <!-- override rule: <link> nodes get special treatment --> 
    <xsl:template match="description//link"> 
    <a href="#{@ref}"> 
     <xsl:apply-templates /> 
    </a> 
    </xsl:template> 

    <!-- default rule: ignore any unspecific text node --> 
    <xsl:template match="text()" /> 

    <!-- override rule: copy any text node beneath description --> 
    <xsl:template match="description//text()"> 
    <xsl:copy-of select="." /> 
    </xsl:template> 

</xsl:stylesheet> 

Kết quả sau đây được tạo cho XML đầu vào của bạn (Lưu ý: Tôi được truyền dẫn qua gọn gàng vì lợi ích của khả năng đọc không phù hợp trắng-không gian là gì. bị xóa trong quá trình):

<h1>List of books</h1> 
<h2><a name="Stuff">Stuff</h2> 
<p>This book is <i>great</i> if you need to know about stuff. I 
suggest <a href="#Things">this one</a> if you need to know about 
things.</p> 
+0

Tôi sẽ không bao giờ ngừng chảy nước dãi. Tôi đoán tôi sẽ phải làm việc một chút khó khăn hơn để có được những trình đơn pesky xây dựng bản thân, cảm ơn rất nhiều! : P –

0
<root> 
<book> 
    <title>Stuff</title> 
    <description><![CDATA[ 
     This book is <i>great</i> if you need to know about stuff. 
     I suggest <link ref="Things">this one</link> if you need to know 
     about things. 
    ]]></description> 
</book> 
[other books] 
</root> 
+0

Có thể là một tùy chọn, mặc dù tôi phải "thay đổi" liên kết của tôi thành thẻ "a", mà thông tin phải được thay đổi tùy thuộc vào một số nội dung. –

+0

Ah, vâng. Đã không nhìn thấy điều đó, xin lỗi. – cadrian

Các vấn đề liên quan