【问题标题】:XSLT: How to replace stringXSLT:如何替换字符串
【发布时间】:2013-12-19 00:32:27
【问题描述】:

我想使用 xslt 对以下 xml 执行两项任务。可以帮忙吗,谢谢。

  1. 有字符串'null'的地方用空字符串替换
  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>

【问题讨论】:

  • 你能发布你在 XSLT 上的尝试来解决这个问题吗?另外,截断是什么意思?您样本中的 SSN 仍然有前导零吗?

标签: string xslt replace truncate


【解决方案1】:

您可以使用identity transform 中的两个专用模板完成您想要的工作。

  1. 使用 xsl:number 或 number() function 删除前导零的 SSN 模板
  2. 匹配任何计算值为“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>

匹配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>

【讨论】:

    猜你喜欢
    • 2011-03-05
    • 2021-10-10
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    相关资源
    最近更新 更多