2013-12-19 4 views
1

Я хочу выполнить две задачи в следующем xml с помощью xslt. вы можете помочь, спасибо.XSLT: Как заменить строку

  1. где есть строка «NULL» заменить его пустой строкой
  2. где ПЛА начинается с нулей укоротить их

Может кто-то поставил меня в правильном направлении, пожалуйста?

Источник 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:number или number() function удалить ведущие нули
  2. шаблон, который соответствует любому элементу, который вычисленное значение «нуль» и копирует элемент без какого-либо из его содержимого.

Applied в следующей таблице стилей:

<?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> 
Смежные вопросы