2012-10-24 4 views
3

설명 요소가 존재하는지 여부를 확인하는 xslt 코드를 작성하려고 시도하면 설명 요소가 표시되지만 설명 요소가없는 경우 설명 요소가 표시되지 않습니다. 하지만 거기에 아무 가치가 없다하더라도 아래 코드는 여전히 요소를 보여줍니다. 서비스에 대한 설명이 없으면 description 요소를 표시하지 않도록 코드를 작성할 수 있습니다.불필요한 요소를 제거하는 XSLT

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


    <xsl:template match="Service"> 
    <xsl:element name="equipment"> 
     <xsl:if test="description !='' "> 
      <xsl:value-of select="description" /> 
     </xsl:if> 
     <xsl:if test="not(description)"> 
     </xsl:if> 
    </xsl:element> 
    </xsl:template> 
    </xsl:stylesheet> 

가 같이있는 returned.i가 비어 있지 첫 번째 2 장비 요소를 반환 할 빈 장비 요소입니다.

+0

는 당신이 XML 출력을 원하십니까? –

+0

예, 출력은 설명이있는 서비스에만 요소를 추가해야합니다. – redtail1541

+0

출력이 유효하지 않습니다 xml .. 단 하나의 루트 요소가 필요합니다 – Treemonkey

답변

0
 <xsl:template match="/"> 
    <xsl:apply-templates select="//Service"/> 
    </xsl:template> 
<xsl:template match="Service"> 
     <xsl:if test="description !='' "> 
      <xsl:element name="equipment"> 
      <xsl:value-of select="description" /> 
    </xsl:element> 
     </xsl:if> 
    </xsl:template> 

또는

<xsl:template match="/"> 
    <xsl:apply-templates select="//Service"/> 
    </xsl:template> 
    <xsl:template match="Service"> 
     <xsl:if test="child::description[text()]"> 
     <xsl:element name="equipment"> 
      <xsl:value-of select="description" /> 
      </xsl:element> 
     </xsl:if> 
    </xsl:template> 
+0

당신을 환영합니다! – Shil

1

업데이트 된 솔루션은 다음과 같습니다.

<xsl:template match="Services"> 
    <xsl:for-each select="Service"> 
     <xsl:if test="count(description) &gt; 0 and description!=''"> 
     <equipment> 
      <xsl:value-of select="description"/> 
     </equipment> 
     </xsl:if> 
    </xsl:for-each> 

    </xsl:template> 


</xsl:stylesheet> 
+0

이것은 내가하려고하는 것이 아닙니다. 나는 내 코드를 확장하려고합니다. 그리고 어쩌면 당신은 내가 뭘 하려는지 이해할 수 있습니다. – redtail1541

+0

주어진 입력 XML에 대해 원하는 출력 XML로 질문을 업데이트 할 수 있습니까? –

+0

원하는 출력을 업데이트했습니다. – redtail1541

0

당신을 위해이 일을합니까 확인하십시오?

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

<!-- place <result /> as root to produce wellformed XML --> 
<xsl:template match="/"> 
    <result><xsl:apply-templates /></result> 
</xsl:template> 

<!-- rewrite those <Service /> that have a <description /> --> 
<xsl:template match="Service[./description]"> 
    <equipment><xsl:value-of select="description" /></equipment> 
</xsl:template> 

<!-- remove those who do not --> 
<xsl:template match="Service[not(./description)]" /> 
</xsl:transform> 
관련 문제