2014-01-09 4 views
2

MS Word에서 많은 HTML 파일을 만들었습니다. 데이터와 기타 등등을 추출하기 위해이 파일의 내용을 조작하려고합니다.혼합 내용 노드에서 외부 노드로 마지막 공간 이동

HTML 단락에는 내용이 서로 섞여 있으며 기울임 꼴 또는 굵은 단어 뒤의 공백은 기울임 꼴로 표시되는 경우가 많습니다. 내가 normalize-space()이되면 나중에 공간이 제거되고 연결되지 않아야하는 단어가 연결됩니다.

<p>Some text here and some <i>italicized </i>text here.</p> 

나중에 변환이되기 위해 원인

<p>Some text here and some <i>italicized</i>text here.</p> 

(I 다소 일을 단순화하고 있습니다.)

나는 경우를 식별 할

<p>Some text here and some <i>italicized</i> text here.</p> 

와 끝까지 할 요소 내부의 마지막 노드는 공백 문자로 끝나는 텍스트 노드이고, 후행 부엉 espace를 추가하고 요소 다음에 공백을 추가하십시오.

나는 뭔가를 함께 쓸 수 있다고 생각하지만 XQuery는 털이 많아지고 나는 더 쉬운 방법이 있다고 생각해야한다. (그렇지 않을 수도 있지만, 묻지 않았다면 나는 바보가 될 것입니다.)

XSLT, finding out if last child node is a specific element 거의 비슷하지만 아주 보입니다.

답변

0
<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0"> 

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

    <!--Match the elements who's last child node is a text() node 
     that ends with a space. --> 
    <xsl:template match="*[node()[last()] 
           [self::text()[substring(.,string-length())=' ']]]"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
     <!--add the extra space following the matched element--> 
     <xsl:text> </xsl:text> 
    </xsl:template> 

    <!--Match the text() node that is the last child node of an element 
     and ends with a space --> 
    <xsl:template match="*/node()[last()] 
           [self::text()[substring(., string-length())=' ']]"> 
     <!--remove the trailing space--> 
     <xsl:value-of select="substring(., 0, string-length())"/> 
    </xsl:template> 

</xsl:stylesheet> 
관련 문제