2015-10-14 4 views
0

Я пытаюсь удалить узел, но вставлять содержимое в узел предков. Это XML:XSLT Удалить узел, но сохранить содержимое

<w lemma="comment2" type="adv." ana="comment">comment</w> 
<name ref="roland"><w lemma="roland" type="nom propre" ana="roland">roland</w></name> 
<w lemma="faire" type="vindps3" ana="fyt"><choice><orig>fyt</orig><reg>fist</reg></choice></w> 
<name ref="yvon de montauban"><w lemma="yvon" type="nom propre" ana="yvon">yvon</w> 
<w lemma="de" type="prép" ana="de">de</w> 

Мое желание удалить completedly <reg> и его содержимое, чтобы удалить тег <choice> и удалить тег <orig> НО поместить его содержимое в <w> тега. Может кто-нибудь, пожалуйста, помогите мне?

XSLT является сейчас, как это:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"> 
    <xsl:output method="xml"/> 
    <xsl:template match="*"> 
     <xsl:element name="{local-name()}"> 
      <xsl:for-each select="./@*"> 
       <xsl:attribute name="{local-name()}"> 
        <xsl:value-of select="."/> 
       </xsl:attribute> 
      </xsl:for-each> 
      <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="div"> 
     <xsl:element name="{local-name()}"> 
      <xsl:for-each select="./@*"> 
       <xsl:attribute name="{local-name()}"> 
        <xsl:value-of select="."/> 
       </xsl:attribute> 
      </xsl:for-each> 
      <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="s"> 
     <xsl:element name="{local-name()}"> 
      <xsl:for-each select="./@*"> 
       <xsl:attribute name="{local-name()}"> 
        <xsl:value-of select="."/> 
       </xsl:attribute> 
      </xsl:for-each> 
      <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="name"> 
     <xsl:element name="{local-name()}"> 
      <xsl:for-each select="./@*"> 
       <xsl:attribute name="{local-name()}"> 
        <xsl:value-of select="."/> 
       </xsl:attribute> 
      </xsl:for-each> 
      <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="pb"> 
     <xsl:choose> 
      <xsl:when test="@ed='bnf'"> 
       <xsl:element name="{local-name()}"> 
        <xsl:for-each select="./@*"> 
         <xsl:attribute name="{local-name()}"> 
          <xsl:value-of select="."/> 
         </xsl:attribute> 
        </xsl:for-each> 
        <xsl:apply-templates/> 
       </xsl:element> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:apply-templates/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
    <xsl:template match="w"> 
     <xsl:choose> 
      <xsl:when test="descendant::orig"> 
       <xsl:element name="w"> 
        <xsl:for-each select="./@*"> 
         <xsl:attribute name="{local-name()}"> 
          <xsl:value-of select="."/> 
         </xsl:attribute> 
        </xsl:for-each> 
        <xsl:apply-templates/> 
       </xsl:element> 
      </xsl:when> 
      <xsl:when test="descendant::reg"> 
        <xsl:apply-templates/> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:element name="w"> 
        <xsl:for-each select="./@*"> 
         <xsl:attribute name="{local-name()}"> 
          <xsl:value-of select="."/> 
         </xsl:attribute> 
        </xsl:for-each> 
        <xsl:apply-templates/> 
       </xsl:element> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
    <xsl:template match="choice"/> 
    <xsl:template match="orig"> 
     <xsl:apply-templates select="*" /> 
     <xsl:apply-templates/> 
    </xsl:template> 
    <xsl:template match="reg"/> 
</xsl:stylesheet> 

Спасибо за вашу помощь :) Михи

+0

Мое желание удалить completedly REG-тег и его содержимое, чтобы удалить выбор-теги и удалить Orig-тег НО поместить его содержимое в w-tag. – Micha

+0

Ваш XML не является корректным. Вы не представили ожидаемый результат. Пожалуйста, обновите свой вопрос. –

ответ

0

Пусть даны хорошо сформированный вход XML:

<root> 
    <w lemma="comment2" type="adv." ana="comment">comment</w> 
    <name ref="roland"> 
    <w lemma="roland" type="nom propre" ana="roland">roland</w> 
    </name> 
    <w lemma="faire" type="vindps3" ana="fyt"> 
    <choice> 
     <orig>fyt</orig> 
     <reg>fist</reg> 
    </choice> 
    </w> 
    <name ref="yvon de montauban"> 
    <w lemma="yvon" type="nom propre" ana="yvon">yvon</w> 
    <w lemma="de" type="prép" ana="de">de</w> 
    </name> 
</root> 

нижеследующая таблица стилей:

XSLT-1,0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="choice|orig"> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="reg"/> 

</xsl:stylesheet> 

вернется:

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <w lemma="comment2" type="adv." ana="comment">comment</w> 
    <name ref="roland"> 
     <w lemma="roland" type="nom propre" ana="roland">roland</w> 
    </name> 
    <w lemma="faire" type="vindps3" ana="fyt">fyt</w> 
    <name ref="yvon de montauban"> 
     <w lemma="yvon" type="nom propre" ana="yvon">yvon</w> 
     <w lemma="de" type="prép" ana="de">de</w> 
    </name> 
</root> 

Примечание:

  1. choice обертка была удалена;
  2. Содержание orig стало содержимым предка w узел;
  3. Весь узел reg был подавлен.
0

Я знаю, что это не ответ, но он не может быть отформатирован в поле комментариев.

Ваш XSLT-код может быть улучшен настолько! Например, вместо

<xsl:template match="w"> 
     <xsl:choose> 
      <xsl:when test="descendant::orig"> 
       <xsl:element name="w"> 
        <xsl:for-each select="./@*"> 
         <xsl:attribute name="{local-name()}"> 
          <xsl:value-of select="."/> 
         </xsl:attribute> 
        </xsl:for-each> 
        <xsl:apply-templates/> 
       </xsl:element> 
      </xsl:when> 

вы могли бы написать

<xsl:template match="w[.//orig]"> 
    <w> 
    <xsl:copy-of select="@*"/> 
    <xsl:apply-templates/> 
    </w> 
</xsl:template> 
Смежные вопросы