2013-12-19 72 views
1

我想使用xslt對以下xml執行兩項任務。可以請你幫忙,謝謝。XSLT:如何替換字符串

  1. 哪裏有字符串「空」由空字符串替換它
  2. 其中SSN以零開始截斷他們

有人可以把我在正確的方向嗎?

源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

您可以發佈您試圖在XSLT來解決這個問題?另外,你是什麼意思截斷?樣本中的SSN仍然有前導零? – smj

回答

0

可以完成你想要在一個identity transform兩個專門的模板是什麼。

  1. 使用或者xsl:numbernumber() function刪除前導零
  2. 是誰的計算值的任何元素相匹配的模板是「空」,並複製件,沒有任何的它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()節點,而不是它們的元素上匹配的較短的版本:

<?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>