2009-08-27 95 views
1

我必須使該屬性爲單個元素。請指導我。如何在必要位置將序列屬性轉換爲單個元素?

這裏我的輸入和要求。

輸入:

<book pagenum="01"> 
<title pagenum="01">Book title</title> 
<chapter pagenum="02"> 
<chaptertitle pagenum="02">CHAPTER TITLE</chaptertitle> 
<section pagenum="03"> 
<sectiontitle pagenum="03">SECTION TITLE</chaptertitle> 
<subsection pagenum="04"> 
<para pagenum="04">body content</para> 
<para pagenum="04">body content</para> 
<para pagenum="04">body content</para> 
<para pagenum="05">body content</para> 
<para pagenum="05">body content</para> 
<para pagenum="05">body content</para> 
<para pagenum="06">body content</para> 
<para pagenum="06">body content</para> 
</subsection></section></chapter></book> 

輸出:

<book> 
<?docpage num="01"?><pagenum id="p01">01</pagenum> 
<booktitle>Book title</booktitle> 
<chapter> 
<?docpage num="02"?><pagenum id="p02">02</pagenum> 
<chaptertitle>CHAPTER TITLE</chaptertitle> 
<section> 
<?docpage num="03"?><pagenum id="p03">03</pagenum> 
<sectiontitle>SECTION TITLE</chaptertitle> 
<subsection> 
<?docpage num="04"?><pagenum id="p04">04</pagenum> 
<para>body content</para> 
<para>body content</para> 
<para>body content</para> 
<?docpage num="05"?><pagenum id="p05">05</pagenum> 
<para>body content</para> 
<para>body content</para> 
<para>body content</para> 
<?docpage num="06"?><pagenum id="p06">06</pagenum> 
<para>body content</para> 
<para>body content</para> 
</subsection></section></chapter></book> 

如何將此轉換的XSLT腳本...?請指導我。

感謝, 邁克爾

+0

下一次,請使用代碼格式化('工具欄上的按鈕0101')來正確地格式化代碼 - XML這是因爲否則就無法閱讀非常重要。 – 2009-08-27 07:40:05

+0

對不起,帕維爾,我的力量讓你..!你的意思是屬性值..?你能再來嗎..? – Micheal 2009-08-27 07:47:29

+0

當您的問題(或答案)有一些代碼時,爲了使其格式正確,您需要以4個空格開始每行。在編輯器中,表示「0101」的按鈕將爲當前選擇做到這一點。對於這個,我編輯它來修復格式;下面是它之前的樣子:http://stackoverflow.com/posts/1339431/edit/16f34505-38ed-410c-bb26-5a84dc0ed2ab – 2009-08-27 07:52:12

回答

2

東西沿着這些路線:

<!-- Identity transform - copy all elements as is by default --> 
<xsl:template match="node() | @*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node() | @*" /> 
    </xsl:copy> 
</xsl:template> 

<!-- Match any element for which @pagenum is different from preceding one. 
    Note that <para> is handled specially below. --> 
<xsl:template match="*[@pagenum != preceding::*[1]/@pagenum]"> 
    <xsl:copy> 
    <!-- Insert the PI and <pagenum> as first child --> 
    <xsl:call-template name="insert-pagenum"/> 
    <xsl:apply-templates select="node() | @*" /> 
    </xsl:copy> 
</xsl:template> 

<!-- Match <para> for which @pagenum is different from preceding one. --> 
<xsl:template match="para[@pagenum != preceding::*[1]/@pagenum]"> 
    <!-- Insert the PI and <pagenum> before the opening tag of <para> --> 
    <xsl:call-template name="insert-pagenum"/> 
    <xsl:copy> 
    <xsl:apply-templates select="node() | @*" /> 
    </xsl:copy> 
</xsl:template> 

<xsl:template name="insert-pagenum"> 
    <xsl:processing-instruction name="docpage"> 
    <xsl:text>num=</xsl:text><xsl:value-of select="@pagenum"/> 
    </xsl:processing-instruction> 
    <pagenum id="p{@pagenum}"> 
    <xsl:value-of select="@pagenum"/> 
    </pagenum> 
</xsl:template> 
+0

非常感謝Pavel ..它工作正常! :) – Micheal 2009-08-27 08:11:15

+0

邁克爾馬克帕維爾的答案被接受(複選標記),如果它爲你工作。也upvote他:) – 2009-08-27 08:21:01

+0

對於信息,「先行::」是這樣做的一個非常昂貴的方式; Muenchian方法(帶「鑰匙」)要快得多。 – 2009-08-27 08:33:47

2

所以你只想寫@pagenum第一個每個嗎?喜歡的東西:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:key name="pages" match="//@pagenum" use="." /> 

    <xsl:template match="@* | node()"> 
    <xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy> 
    </xsl:template> 
    <xsl:template match="@pagenum"> 
    <xsl:if test="generate-id() = generate-id(key('pages', .))"> 
     <xsl:processing-instruction name="docpage">num=<xsl:value-of select="."/></xsl:processing-instruction> 
     <pagenum id="p{.}"><xsl:value-of select="."/></pagenum> 
    </xsl:if> 
    </xsl:template> 
    <xsl:template match="title"> 
    <booktitle><xsl:apply-templates select="@* | node()"/></booktitle> 
    </xsl:template> 
</xsl:stylesheet> 
+0

+1,這是方法上優越的解決方案。雖然我可能會更嚴格地縮進它,但時間更短。 – Tomalak 2009-08-27 09:18:23

+1

有關信息,「generate-id()= generate-id(key('pages',。))」行意味着「我是第一個? – 2009-08-27 09:31:53

+0

這將始終將生成的元素作爲我們擁有@pagenum的元素的子元素。如果仔細查看示例輸出,對於''這不是真的 - 因爲它必須將生成的元素放在元素的前面,而不是放在元素的內部。 – 2009-08-27 16:11:00

相關問題