2013-12-19 2 views
1

xslt를 사용하여 다음 xml에 대해 두 가지 작업을 수행하려고합니다. 도와 주시겠습니까? 고마워요.XSLT : 문자열 바꾸기 방법

  1. 어디 SSN은 제로로 시작
  2. 은 잘라 문자열 '널 (null)이'빈 문자열로 대체되어 그들을 거기

사람이 올바른 방향으로 제발 할 수 있나요?

소스 XML :

<?xml version='1.0'?> 
<!-- This file represents a fragment of a book store inventory database --> 
<bookstore> 
    <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0"> 
     <title>The Autobiography of Benjamin Franklin</title> 
     <author> 
      <first-name>Benjamin</first-name> 
      <last-name>Franklin</last-name> 
      <SSN>0001111</SSN> 
      <address></address> 
     </author> 
     <price>8.99</price> 
    </book> 
    <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2"> 
     <title>The Confidence Man</title> 
     <author> 
      <first-name>Herman</first-name> 
      <last-name>Melville</last-name> 
      <SSN>0001112</SSN> 
      <address></address> 
     </author> 
     <price>11.99</price> 
    </book> 
    <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6"> 
     <title>The Gorgias</title> 
     <author> 
      <first-name>JJ</first-name> 
      <last-name>MM</last-name> 
      <SSN>0001113</SSN> 
      <address>null</address> 
     </author> 
     <price>5.99</price> 
    </book> 
</bookstore>  

결과 XML 샘플

<bookstore> 
    <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0"> 
     <title>The Autobiography of Benjamin Franklin</title> 
     <author> 
      <first-name>Benjamin</first-name> 
      <last-name>Franklin</last-name> 
      <SSN>0001112</SSN> 
      <address></address> 
     </author> 
     <price>8.99</price> 
    </book> 

    ... 

</bookstore> 
+1

? 또한, 잘 렸음을 의미합니까? 샘플의 SSN에 여전히 0이 붙어 있습니까? – smj

답변

0

당신은 당신이 identity transform 두 전문 템플릿 원하는 것을 수행 할 수 있습니다.

  1. 그것의 어떤없이 최고의 가치를 계산있어 모든 요소를 ​​일치하는 템플릿이 "널 (null)"입니다 제로
  2. 복사 요소를 제거하거나 xsl:number 또는 number() function를 사용 SSN을위한 템플릿 콘텐츠입니다. 다음과 같은 스타일의

응용 :

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

    <!--identity template, which will copy all content by default--> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <!--For all SSN elements, 
     copy the matched element and use xsl:number to 
     remove any leading zeros from the value --> 
    <xsl:template match="SSN"> 
     <xsl:copy> 
      <xsl:number value="."/> 
     </xsl:copy> 
    </xsl:template> 

    <!--for any element who's computed value is "null", 
     copy the element and do not copy it's value(removing "null")--> 
    <xsl:template match="*[.='null']"> 
     <xsl:copy/> 
    </xsl:template> 

</xsl:stylesheet> 

대신 그 요소의 text() 노드에 일치하는 짧은 버전 : 당신은 당신이 문제를 해결하기 위해 XSLT에서 시도 게시 할 수

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 
    <!--identity template, which will copy all content by default--> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <!--For all text() nodes of SSN elements, 
     Use xsl:number to remove any leading zeros from the value --> 
    <xsl:template match="SSN/text()"> 
     <xsl:number value="."/> 
    </xsl:template> 

    <!--for any text() node who's value is "null", suppress it from the output--> 
    <xsl:template match="*/text()[.='null']"/> 

</xsl:stylesheet>