2014-02-05 2 views
0

이외의 텍스트가 있습니다.확인 부모 만약 내가 이것과 유사한 XML이 그 아이

부모 요소에 자식이 아닌 텍스트가 있는지 알고 싶습니다.

나는이 솔루션을 마련했습니다는 인수의로 정상화 공간 함수는 순서를 허용하지 않기 때문에

<xsl:if test="normalize-space(parent/child[1]) = normalize-space(parent)"> 
    <xsl:text>No parent text</xsl:text> 
</xsl:if> 

내가 parent/child[1]를 사용합니다.

더 좋은 방법이 있나요?

(나는 주제에 this 질문을 찾았지만 대답이 잘못되었거나 문제는 다른)

답변

1

사용 text() 명시 적으로 노드 텍스트를 참조하십시오.

<xsl:if test="parent/text()[not(parent::child)]"> 
    <!--This parent element has text nodes--> 
</xsl:if> 

아니면 별도의 템플릿을 작성할 수 있습니다 :

이 작업을 수행하는 더 좋은 방법은이 템플릿은

<xsl:template match="text()[parent::parent]"> 
    <xsl:text>parent element with text nodes!</xsl:text> 
</xsl:template> 
+0

부모/텍스트() 부모의 텍스트 플러스는 어린이의 모든의 연결을 반환합니다. – gombost

+0

죄송 합니다만, 연결이 아닙니다. 노드의 텍스트와 자식을 포함하는 여러 텍스트 노드를 반환합니다. 따라서 내 솔루션의 정규화 된 공간에서는 작동하지 않습니다. – gombost

1

:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 
<xsl:strip-space elements="*"/>  
    <xsl:template match="/"> 
     <xsl:choose> 
      <xsl:when test="parent/child::text()"> 
       true 
      </xsl:when> 
      <xsl:otherwise>false</xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

에 적용하여 XML을 입력하면

을 반환합니다. 다음 XML에 적용

:

<?xml version="1.0" encoding="UTF-8"?> 
<parent> 
    <child>child's text</child> 
    <child>other child's text</child> 
</parent> 

반환

false 
관련 문제