我想使用xslt在以下xml上执行两项任务。能帮忙吗,谢谢。
有人可以把我放在正确的方向吗?
源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>
答案 0 :(得分:0)
您可以使用identity transform中的两个专业模板来完成您想要的任务。
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()
节点上匹配的较短版本,而不是其元素:
<?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>