2013-04-23 2 views
0

책 이름이 'NC'에 해당 될 때 음수 값을 양수로 바꾸는 작은 일반 변환을 XSL 1.0에 작성해야합니다. 가격은 여러 수준에서 발생할 수 있습니다. 나는 부정적인 징후를 만나서 모든 종류의 XML에 대해 긍정적으로 전환해야합니다. 제발 제안 해주세요.음수 가격을 양수로 바꾸기

XML-

당신이 'NC'의 이름을 가진 요소를 예약 속한 다수의 제로보다 작은 경우로 텍스트 가격 요소를 원하는 경우
<Books> 
<Book> 
    <Name>NC</Name> 
    <Price>-100.50</Price> 
</Book> 
<Book> 
    <Name>B1</Name> 
    <Pr>450.60</Pr> 
</Book> 
<Book> 
    <Name>C1</Name> 
    <Price>35.20</Price> 
</Book> 
<Book> 
    <Name>D1</Name> 
    <P>5</P> 
</Book> 
</Books> 
+0

[시도한 내용] (http://www.whathaveyoutried.com/)을 표시하십시오. – Borodin

답변

1

. 이 경우 템플릿 일치가 필요합니다.

<xsl:template match="Book[Name='NC']/Price[number(.) &lt; 0]/text()"> 

음수 값을 양수로 설정하는 코드를 추가하면됩니다. 당신의 XML에 적용하면

는 다음 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" /> 

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

<xsl:template match="Book[Name='NC']/Price[number(.) &lt; 0]/text()"> 
    <xsl:value-of select="format-number(0 - number(.), '0.00')" /> 
</xsl:template> 

</xsl:stylesheet> 

을 시도, 다음과 같은 출력

<Books> 
    <Book> 
     <Name>NC</Name> 
     <Price>100.50</Price> 
    </Book> 
    <Book> 
     <Name>B1</Name> 
     <Pr>450.60</Pr> 
    </Book> 
    <Book> 
     <Name>C1</Name> 
     <Price>35.20</Price> 
    </Book> 
    <Book> 
     <Name>D1</Name> 
     <P>5</P> 
    </Book> 
</Books> 

참고 기존 요소를 복사 할 identity transform의 사용이다.

관련 문제