String XSLT:如何替换字符串
我想使用xslt对以下xml执行两项任务。你能帮忙吗,谢谢 如果存在字符串“null”,请将其替换为空字符串 其中SSN以零开始,截断它们 有人能给我指出正确的方向吗 源XML:String XSLT:如何替换字符串,string,xslt,replace,truncate,String,Xslt,Replace,Truncate,我想使用xslt对以下xml执行两项任务。你能帮忙吗,谢谢 如果存在字符串“null”,请将其替换为空字符串 其中SSN以零开始,截断它们 有人能给我指出正确的方向吗 源XML: <?xml version='1.0'?> <!-- This file represents a fragment of a book store inventory database --> <bookstore> <book genre="autobiograph
<?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>
您可以在一个应用程序中使用两个专门的模板来完成所需的操作 SSN的模板,使用或删除前导零 匹配任何计算值的元素的模板为null,并复制该元素而不包含其任何内容。 在以下样式表中应用:
<?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>
您能在XSLT上发布您的尝试来解决这个问题吗?还有,你说的截断是什么意思?样本中的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 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>