2013-08-27 78 views
0

我有要求查找所有文本節點並創建新節點序列(請參閱輸入/輸出XML),並且如果有任何內聯節點(文本節點的前同胞或後同胞) ,然後將這些標記重命名爲< x id =「XX」local-name ='tagName'> text here </x >其中「local-name」值應該是該節點的名稱。這些內聯節點可以是任何東西,而不是特定的列表。任何指針/解決方案將是一個很大的幫助。謝謝。查找所有文本節點和同級

XSLT 2.0版 XSLT處理器 - 撒克遜EE/HE 9.XXX

<?xml version="1.0" encoding="utf-8"?> 
    <concept id="001" xml:lang="en-us"> 
     <title id="002">Notice</title> 
     <shortdesc id="003">This information U.S.A.</shortdesc> 
     <conbody id="004"> 
      <p id="005">This product blah blah <companyname id="006">blah bla</companyname> No other 
       warranty expressed or implied. </p> 
      <p id="007">This supersedes all previous notices.</p> 
      <section id="008"> 
       <title id="009">COPYRIGHT LICENSE:</title> 
       <p id="010">This information contains <b id="011">in source language</b> , blah blah</p> 
      </section> 
     </conbody> 
    </concept> 

預期輸出:

<?xml version="1.0" encoding="UTF-8"?> 
    <root> 
     <trans-unit id="002"> 
      <source>Notice</source> 
      <target>Notice</target> 
     </trans-unit> 
     <trans-unit id="003"> 
      <source>This information U.S.A.</source> 
      <target>This information U.S.A.</target> 
     </trans-unit> 
     <trans-unit id="005"> 
      <source>This product blah blah <x id="006" local-name="companyname">blah bla</x> No other 
       warranty expressed or implied. </source> 
      <target>This product blah blah <x id="006" local-name="companyname">blah bla</x> No other 
       warranty expressed or implied.</target> 
     </trans-unit> 
     <trans-unit id="007"> 
      <source>This supersedes all previous notices.</source> 
      <target>This supersedes all previous notices.</target> 
     </trans-unit> 
     <trans-unit id="009"> 
      <source>COPYRIGHT LICENSE:</source> 
      <target>COPYRIGHT LICENSE:</target> 
     </trans-unit> 
     <trans-unit id="010"> 
      <source>This information contains <x id="011" local-name="b">in source language</x> , blah 
       blah</source> 
      <target>This information contains <x id="011" local-name="b">in source language</x> , blah 
       blah</target> 
     </trans-unit> 
    </root> 

我想是這樣的:

 <xsl:template match="/"> 
      <root> 
      <xsl:for-each select="//text()"> 
       <xsl:if test=".!='' or .!=' '"> 
       <xsl:choose> 
        <xsl:when test="not(following-sibling::node()) or not(preceding-sibling::node())"> 
         <trans-unit> 
          <xsl:attribute name="id"> 
           <xsl:value-of select="../@id"/> 
          </xsl:attribute> 
          <source> 
           <xsl:value-of select="."/>  
          </source> 
          <target> 
           <xsl:value-of select="."/>  
          </target> 
         </trans-unit>      
        </xsl:when>     
       </xsl:choose> 
       </xsl:if> 
      </xsl:for-each> 
      </root> 

回答

1

的樣式表

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

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

<xsl:template match="concept"> 
    <root> 
    <xsl:apply-templates/> 
    </root> 
</xsl:template> 

<xsl:template match="concept//*[text()[normalize-space()]]"> 
    <transunit id="{@id}"> 
    <source> 
     <xsl:apply-templates/> 
    </source> 
    <target> 
     <xsl:apply-templates/> 
    </target> 
    </transunit> 
</xsl:template> 

<xsl:template match="*[not(*)][following-sibling::node()[1][self::text()[normalize-space()]] | 
         preceding-sibling::node()[1][self::text()[normalize-space()]]]" priority="5"> 
    <x id="{@id}" local-name="{local-name()}"> 
    <xsl:apply-templates/> 
    </x> 
</xsl:template> 

</xsl:stylesheet> 

將您輸入到結果

<root> 
    <transunit id="002"> 
     <source>Notice</source> 
     <target>Notice</target> 
    </transunit> 
    <transunit id="003"> 
     <source>This information U.S.A.</source> 
     <target>This information U.S.A.</target> 
    </transunit> 
    <transunit id="005"> 
     <source>This product blah blah <x id="006" local-name="companyname">blah bla</x> No other 
       warranty expressed or implied. </source> 
     <target>This product blah blah <x id="006" local-name="companyname">blah bla</x> No other 
       warranty expressed or implied. </target> 
    </transunit> 
    <transunit id="007"> 
     <source>This supersedes all previous notices.</source> 
     <target>This supersedes all previous notices.</target> 
    </transunit> 
    <transunit id="009"> 
     <source>COPYRIGHT LICENSE:</source> 
     <target>COPYRIGHT LICENSE:</target> 
    </transunit> 
    <transunit id="010"> 
     <source>This information contains <x id="011" local-name="b">in source language</x> , blah blah</source> 
     <target>This information contains <x id="011" local-name="b">in source language</x> , blah blah</target> 
    </transunit> 
</root> 

與撒克遜9.5測試。

+0

感謝martin,它的作品非常適合我的XML。 – Pankaj