2014-09-03 56 views
0

我有以下XML,並希望刪除所有ELE標籤,編號駐留在一些ELE標籤的pH值元素的x屬性之前:編號屬性

<?xml version="1.0" encoding="UTF-8"?> 
    <tmx version="1.4"> 
<body> 
    <tu> 
     <prop type="x-Context">-2050338055591740051, -2050338055591740051</prop> 
     <prop type="x-Origin">TM</prop> 
     <prop type="x-ConfirmationLevel">Translated</prop> 
     <tuv> 
      <seg> 
       <ele>The text </ele> 
       <ele> 
        <ph x="0" type="QIAsymphony"/> 
       </ele> 
       <ele> goes </ele> 
       <ele> 
        <ph x="0" type="470"/> 
       </ele> 
       <ele> here </ele> 
       <ele> 
        <ph x="0" type="471"/> 
       </ele> 
       <ele>.</ele> 
      </seg> 
     </tuv> 
     <tuv> 
      <seg> 
       <ele>El texto </ele> 
       <ele> 
        <ph x="0" type="QIAsymphony"/> 
       </ele> 
       <ele> se mete </ele> 
       <ele> 
        <ph x="0" type="471"/> 
       </ele> 
       <ele> aquí </ele> 
       <ele> 
        <ph x="0" type="470"/> 
       </ele> 
       <ele>.</ele> 
      </seg> 
     </tuv> 
    </tu> 
</body> 
    </tmx> 

我有以下XSLT的,則執行編號賦予動作,雖然我不知道什麼樣的變化,使替換丟失ELE標籤,一旦這個元素已經從XML刪除:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
<xsl:output method="xml" indent="yes" /> 

<xsl:key name="ph" match="tuv[1]/seg/ele/ph" use="@type" /> 

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

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

<xsl:template match="tuv/seg/ele/ph/@x"> 
    <xsl:attribute name="x"> 
     <xsl:value-of select="count(key('ph', ../@type)/../preceding-sibling::ele[ph]) + 1" /> 
    </xsl:attribute> 
</xsl:template> 
</xsl:stylesheet> 

所需的輸出:

<tuv> 
    <seg>The text 
     <ph x="1" type="QIAsymphony"/> 
     goes 
     <ph x="3" type="471"/> 
     here 
     <ph x="2" type="470"/> 
     . 
    </seg> 
</tuv> 
+0

我不明白的問題 - 當前的XSLT沒有刪除任何東西,它只是調整'的x'屬性' ph'元素。如果您可以編輯問題並添加一個您希望產生的輸出樣本,則可能會更清楚,因此我們可以更清楚地看到需要更改的內容。 – 2014-09-03 13:43:40

+0

我會改變。我希望能夠對上述XML執行此編號操作,但畢竟標籤已被刪除。我想知道我需要對上述XSLT進行哪些更改才能實現此目的。 – user3289842 2014-09-03 14:19:00

+0

以下是(部分)所需輸出: El texto se mete aquí。 \t \t \t \t \t \t \t user3289842 2014-09-03 14:22:30

回答

3

在你說的評論中,你想刪除<ele>元素,但不是他們的內容。

因爲你的XSLT已經基於身份的變換,這是很容易通過添加一個非常簡單的模板來完成:

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

此模板簡單地讀作:「當遇到<ele>,不輸出任何東西,但過程其子女「。實際上<ele>已從輸出中刪除,其子項保留。

在背景:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" indent="yes" /> 

    <xsl:key name="ph" match="tuv[1]/seg/ele/ph" use="@type" /> 

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

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

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

    <xsl:template match="tuv/seg/ele/ph/@x"> 
    <xsl:attribute name="x"> 
     <xsl:value-of select="count(key('ph', ../@type)/../preceding-sibling::ele[ph]) + 1" /> 
    </xsl:attribute> 
    </xsl:template> 
</xsl:stylesheet> 

結果:

<tmx version="1.4"> 
    <body> 
    <tu> 
     <prop type="x-Context">-2050338055591740051, -2050338055591740051</prop> 
     <prop type="x-Origin">TM</prop> 
     <prop type="x-ConfirmationLevel">Translated</prop> 
     <tuv> 
     <seg>The text <ph x="1" type="QIAsymphony"/> goes <ph x="2" type="470"/> here <ph x="3" type="471"/>.</seg> 
     </tuv> 
     <tuv> 
     <seg>El texto <ph x="1" type="QIAsymphony"/> se mete <ph x="3" type="471"/> aquí <ph x="2" type="470"/>.</seg> 
     </tuv> 
    </tu> 
    </body> 
</tmx> 
+0

假設我有多個「tu」節點:我如何才能在節點上逐個節點地工作,忽略其他「tu」節點中「ph」元素「type」屬性值的出現? – user3289842 2014-09-09 09:08:02

+0

您必須創建密鑰的''節點ID部分。目前,所有''都按其「@類型」分組,不管它們在文檔中的位置。爲每個''做一個單獨的組,使用這個表達式作爲鍵:'concat(@type,'|',generate-id(ancestor :: tu [1]))''。 (如果你不確定XSL鍵是如何工作的,[請在這裏閱讀下面的答案](http://stackoverflow.com/a/955527/18771)。) – Tomalak 2014-09-09 09:18:15

+0

感謝您的輸入,但我無法理解所提供鏈接中的答案。我甚至不確定關鍵表達應該在哪裏。 – user3289842 2014-09-09 12:40:22