2015-10-14 86 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标签和它的内容,删除选择标签并删除原稿 - 标签,但把它的内容分成w标签。 – 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>