2012-08-04 21 views
3

如何在xslt中剪切和粘貼?想我這樣的XML,XSLT -Cut and Paste

<root> 
    <header> 
     some nodes 
    </header> 
    <body> 
    <p> 
     some text 
    <link>1</link> 
     <note xml:id="c5-note-0001" numbered="no">text</note> 
    </p> 
    <p> 
     some text 
     <link>2</link> 
     <figure> 
     <note xml:id="c5-note-0003">text</note> 
     </figure> 
     <note xml:id="c5-note-0002">text</note> 
    </p> 
    <tabular> 
     <note xml:id="c5-note-0004" numbered="no">text</note> 
    </tabular> 
    <p> 
     some text 
     <link>3</link> 
     <notegroup> 
     <note xml:id="c5-note-0006">text</note> 
     </notegroup> 
     <note xml:id="c5-note-0005">text</note> 
    </p> 
    <p> 
     some text 
     <link>4</link> 
     <note xml:id="c5-note-0007">text</note> 
    </p> 
    </body> 
    </root> 

我的預期產量,

<root> 
    <header> 
     some nodes 
    </header> 
    <body> 
     <p> 
     some text 
     <link>1</link> 
     <note xml:id="c5-note-0001" numbered="no">text</note> 
     </p> 
     <p> 
     some text 
     <link>2</link> 
     <figure> 
      <note xml:id="c5-note-0003">text</note> 
     </figure> 
     <notenum>1</notenum> 
     </p> 
     <tabular> 
     <note xml:id="c5-note-0004" numbered="no">text</note> 
     </tabular> 
     <p> 
     some text 
     <link>3</link> 
     <notegroup> 
      <note xml:id="c5-note-0006">text</note> 
     </notegroup> 
     <notenum>2</notenum> 
     </p> 
     <p> 
     some text 
     <link>4</link> 
     <notenum>3</notenum> 
     </p> 
     <newnote> 
     <note xml:id="c5-note-0002">text</note> 
     <note xml:id="c5-note-0005">text</note> 
     <note xml:id="c5-note-0007">text</note> 
     </newnote> 
    </body> 
    </root> 

我需要body標籤結束之前創建一個新的節點newnote和削減和note節點粘貼到這一點,需要生成一個notenum節點而不是那個note

我只需要在p節點內執行此操作。如果note屬於tabularfigurenotegroup則不需要做任何事情。

如果note包含像numbered="no"那樣的屬性,則不需要做任何事情。

我使用下面的XSLT(只是爲了顯示我使用的模板匹配),

<?xml version="1.0" encoding="utf-8"?> 
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
    > 
     <xsl:output method="xml" indent="yes"/> 

     <xsl:template match="@* | node()"> 
      <xsl:copy> 
       <xsl:apply-templates select="@* | node()"/> 
      </xsl:copy> 
     </xsl:template> 
    <xsl:template match ="figure"> 
     some operation 
    </xsl:template> 
    <xsl:template match ="tabular"> 
     some operation 
    </xsl:template> 
    <xsl:template match ="notegroup"> 
     some operation 
    </xsl:template> 
    </xsl:stylesheet> 

在此先感謝。

+0

我的預期輸出中沒有看到任何''元素,即使據說(根據您的描述文本)生成了該元素。另外,你的意思是*剪切和粘貼*或*複製和粘貼*關於''元素? – 2012-08-04 10:24:12

+0

count節點意味着'notenum',而不是'note'。我需要做剪切和粘貼。請參閱我的輸入和預期輸出。 – dinakar 2012-08-04 10:31:20

+0

我現在看到;弄糊塗了各種''元素被移動或不移動。我已經添加了一個適當的答案,希望能夠滿足你的需求。 – 2012-08-04 10:56:30

回答

2

一般來說,在XSLT中想象「剪切和粘貼」就像它的實際情況一樣,即「複製並粘貼並刪除原始文檔」。

我需要body標籤結束之前創建一個新的節點newnote和剪切和粘貼的說明節點成

你想要做的就是適應<body>元素,以便它包含什麼,在其(可能以其他方式修改)原始內容之後,新的<newnote>元素。因此,添加一個新的模板爲<body>元素:

<xsl:template match="body"> 
</xsl:template> 

正如前面提到的,要基本上接管了原來的內容。作爲原始內容仍然可以通過其他模板進行處理,我們將在這裏使用<xsl:apply-tepmlates>

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

原始內容後,要插入新的元素:

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

最後,在這元素,你希望所有的描述<note>元素的副本,它可以與<xsl:for-each>循環來實現:

<xsl:template match="body"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    <newnote> 
     <xsl:for-each select="p/note[not(@numbered = 'no')]"> 
      <xsl:copy-of select="."/> 
     </xsl:for-each> 
    </newnote> 
</xsl:template> 

並且需要生成notenum節點而不是該音符。

這可以用模板替換相應<note>元件來完成:

<xsl:template match="p/note[not(@numbered = 'no')]"> 
    <notenum><xsl:value-of select="count(preceding::note[parent::p][not(@numbered = 'no')]) + 1"/></notenum> 
</xsl:template> 

插入新<notenum>元件代替並使用<xsl:value-of>命令以輸出計算值。值是匹配您的限制的前面<note>元素的數量加1。

+0

非常感謝O. R. Mapper – dinakar 2012-08-04 12:44:04