개체를 문자열로 변환합니다.
string string(object?)
주의
노드 집합을 문자열로 변환
string() 함수는 노드 집합에서 첫 번째 노드의 문자열 값을 반환하여 노드 집합을 문자열로 변환합니다. 일부 인스턴스에서는 예기치 않은 결과가 나타날 수 있습니다. 예를 들어, 아래의 <test> 노드에 있을 경우
<test>
<item>Apple</item>
<item>Banana</item>
<item>Orange</item>
</test>
다음 string() 함수 호출은 첫 번째 노드 문자열("Apple")을 반환합니다.
string(//text())
string() 함수를 사용하여 모든 자식 텍스트를 연결하려면 노드 집합 대신 단일 노드를 전달해야 합니다. 예를 들어, 다음 string() 함수 호출은 "AppleBananaOrange"를 반환합니다.
string(.)
이 동작은 문자열 인수를 사용하는 모든 XPath 함수에 해당됩니다. 예를 들어, 다음 contains() 함수 호출은
contains(//text(),'Banana')
false 값을 반환합니다. 이 예제에서는 첫 번째 노드 문자열("Apple")만 검색하는 string(//text())
을 사용하여 첫 번째 인수("//text()
")가 문자열로 변환되기 때문에 이러한 동작이 발생합니다. 이와 반대로 contains() 함수는 다음과 같이 첫 번째 인수에 대해 점 선택기(".")를 사용하도록 수정되었습니다.
contains(.,'Banana')
대신 **contains()**는 문자열 "AppleBananaOrange"만 검색하기 때문에 true 값을 반환합니다.
참고
노드 집합이 비어 있으면 빈 문자열이 반환됩니다.
노드 집합 변환 시 공백 처리
다음과 같이 공백이 포함된 노드 집합을 처리할 수 있습니다.
<xsl:strip-space> 요소 명령을 사용하여 공백 노드 제거
<xsl:for-each> 요소 명령을 사용하여 공백 노드 및 텍스트가 포함된 노드 등 모든 하위 노드에서 루프 실행
예를 들어, .//text()
식을 다음 요소에 적용하여 내부 텍스트 내용을 선택한 경우
<name>element</name>
다음과 같이 기본적으로 세 노드의 집합을 반환합니다. 이 노드 집합에서 첫 번째 노드와 세 번째 노드는 실제 텍스트 데이터("element")에 인접한 선행 및 후행 공백 노드를 나타냅니다.
Node 1:   
Node 2: element
Node 03:
공백만 있는 노드를 무시하려면 다음과 같이 XSLT 스타일시트에서 <xsl:strip-space> 명령을 지정합니다.
<xsl:strip-space elements="name"/>
또는 아래 <xsl:for-each> 루프 예제를 사용하여 각 하위 노드에 대해 루프를 실행하고 검색을 반복합니다.
<xsl:for-each select=".//text()">
<xsl:if test="contains(., 'element')" >
…
</xsl:if>
</xsl:for-each>
숫자를 문자열로 변환
다음과 같이 숫자가 문자열로 변환됩니다.
NaN은 문자열 NaN으로 변환됩니다.
양의 0은 문자열 "0"으로 변환됩니다.
음의 0은 문자열 "0"으로 변환됩니다.
양의 무한대는 문자열 "Infinity"로 변환됩니다.
음의 무한대는 문자열 "-Infinity"로 변환됩니다.
숫자가 정수이면 소수점과 앞에 0이 없는 10진수 형식의 숫자로 나타납니다. 숫자가 음수이면 앞에 빼기 기호(-)가 표시됩니다.
그렇지 않으면 소수점 및 소수 자릿수와 정수 자릿수가 하나 이상인 10진수 형식의 숫자로 나타납니다. 숫자가 음수이면 앞에 빼기 기호(-)가 표시됩니다. 0.5와 같이 소수점 앞이 0인 경우를 제외하고 정수 부분에는 0이 없어야 합니다. 소수 부분에는 필요한 자릿수 외에 다른 모든 IEEE 754 숫자 값과 고유하게 구분할 수 있도록 자릿수가 더 필요합니다.
참고
string()
함수는 숫자를 문자열로 변환하여 사용자에게 표시하기 위한 것이 아닙니다. XSLT(XSL Transformations)의 format-number()
함수 및 <xsl:number>
요소가 이 기능을 제공합니다.
부울을 문자열로 변환
부울 false 값은 문자열 "false"로 변환되고 부울 true 값은 문자열 "true"로 변환됩니다.
개체를 문자열로 변환
네 가지 기본 형식 이외의 개체 형식은 해당 형식에 따른 방법에 의해 문자열로 변환됩니다.
이 인수를 생략하면 유일한 멤버로 컨텍스트 노드가 있는 노드 집합을 기본값으로 설정합니다.
예제
다음 예제에서는 XPath 식에서 string()
함수를 사용하는 방법을 보여 줍니다. 두 경우에서(아래 XSLT 파일에서 굵은 글꼴로 표시한 명령 참조) 이 함수를 사용하여 해당 인수가 문자열 식으로 간주되는지 확인합니다.
XML 파일(string.xml)
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl"
href="string.xsl"?>
<arithmetics>
<operation>
<operator>+</operator>
<operand>1</operand>
<operand>2.00</operand>
</operation>
<operation>
<operator>+</operator>
<operand>One</operand>
<operand>2.00</operand>
</operation>
<operation>
<operator>-</operator>
<operand>1</operand>
<operand>2.00</operand>
</operation>
<operation>
<operator>*</operator>
<operand>1</operand>
<operand>2.00</operand>
</operation>
<operation>
<operator>div</operator>
<operand>-1</operand>
<operand>0.0</operand>
</operation>
<operation>
<operator>mod</operator>
<operand>5</operand>
<operand>2</operand>
</operation>
<operation>
<operator>mod</operator>
<operand>5</operand>
<operand>2.5</operand>
</operation>
<operation>
<operator>mod</operator>
<operand>5</operand>
<operand>2.25</operand>
</operation>
<operation>
<operator>&</operator>
<operand>0</operand>
<operand>1</operand>
</operation>
</arithmetics>
XSLT 파일(string.xsl)
<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="string.xsl"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"
omit-xml-declaration="yes"/>
<xsl:template match="/arithmetics">
<html>
<head><title>example</title></head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="operation">
<DIV>
<xsl:choose>
<xsl:when test="string(operator)='+'">
<xsl:apply-templates select="." mode="add"/>
</xsl:when>
<xsl:when test="string(operator)='-'">
<xsl:apply-templates select="." mode="sub"/>
</xsl:when>
<xsl:when test="string(operator)='*'">
<xsl:apply-templates select="." mode="mul"/>
</xsl:when>
<xsl:when test="string(operator)='div'">
<xsl:apply-templates select="." mode="div"/>
</xsl:when>
<xsl:when test="string(operator)='mod'">
<xsl:apply-templates select="." mode="mod"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="." mode="err"/>
</xsl:otherwise>
</xsl:choose>
</DIV>
</xsl:template>
<xsl:template match="operation" mode="show">
<xsl:value-of select="operand[1]"/>  
<xsl:value-of disable-output-escaping="yes"
select="string(operator)"/>  
<xsl:value-of select="operand[2]"/>  
=  
</xsl:template>
<xsl:template match="operation" mode="err">
<xsl:apply-templates select="." mode="show"/>
<xsl:value-of select="string('Invalid arithmetic operation')"/>
</xsl:template>
<xsl:template match="operation" mode="add">
<xsl:apply-templates select="." mode="show"/>
<xsl:value-of select="operand[1] + operand[2]"/>
</xsl:template>
<xsl:template match="operation" mode="sub">
<xsl:apply-templates select="." mode="show"/>
<xsl:value-of select="operand[1] - operand[2]"/>
</xsl:template>
<xsl:template match="operation" mode="mul">
<xsl:apply-templates select="." mode="show"/>
<xsl:value-of select="operand[1] * operand[2]"/>
</xsl:template>
<xsl:template match="operation" mode="div">
<xsl:apply-templates select="." mode="show"/>
<xsl:value-of select="operand[1] div operand[2]"/>
</xsl:template>
<xsl:template match="operation" mode="mod">
<xsl:apply-templates select="." mode="show"/>
<xsl:value-of select="operand[1] mod operand[2]"/>
</xsl:template>
</xsl:stylesheet>
출력
1 + 2.00 = 3
One + 2.00 = NaN
1 - 2.00 = -1
1 * 2.00 = 2
-1 div 0.0 = -Infinity
5 mod 2 = 1
5 mod 2.5 = 0
5 mod 2.25 = 0.5
0 & 1 = Invalid arithmetic operation
참고 항목
참조
XML 데이터 형식 참조
format-number 함수