2013-08-27 5 views
0

모든 텍스트 노드를 찾고 새 노드 시퀀스를 작성해야합니다 (입력/출력 XML 참조). 그리고 인라인 노드 (텍스트 노드의 선행 형제 또는 형제 노드) 그런 다음 해당 태그의 이름을 <으로 바꿉니다. x id = "XX"local-name = 'tagName'> 여기에 텍스트 </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> 

색슨 9.5 테스트 결과

<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> 

입력으로 변환한다.

+0

감사합니다 martin, 그 작품은 아름답게 내 XML을위한. – Pankaj