使用xslt替换元素

时间:2016-10-05 14:03:11

标签: xml xslt xslt-1.0

输入:

<Data>
    <person>
        <id>123456</id>
        <formatid></formatid>
    </person>
    <person>
        <id></id>
        <formatid>****89</formatid>
    </person>
    <person>
        <id>234567</id>
        <formatid>****89</formatid>
    </person>   
</Data>

输出:

<Data>
    <person>
        <formatid>****56</formatid>
    </person>
    <person>
        <formatid>****89</formatid>
    </person>
    <person>
        <formatid>****67</formatid>
    </person>   
</Data>

需要将输入xml转换为输出。条件是

  1. 如果请求有id,则始终发送最后两位数字,但不会     forma formatid有价值与否(第1和第3人)。
  2. 如果请求有空白ID,则按原样发送格式ID。 (人2)
  3. 我的样式表

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:str="http://exslt.org/strings">
    
        <xsl:template match="/">
                <xsl:apply-templates/>
        </xsl:template>
    
        <xsl:template match="id">
            <xsl:variable name="clearid" select="./text()"/>
            <xsl:choose>
                <xsl:when test="$clearid != ''">
                    <xsl:variable name="idLen" select="string-length($clearid)"/>
                    <xsl:variable name="star" select="translate($clearid, '0123456789','**********')"/>
                    <xsl:variable name="idstar" select="concat( substring($star, 1,  $idLen - 1), substring($clearid,  $idLen - 1))"/>          
                    <xsl:element name="formatid">
                        <xsl:value-of select="$idstar"/>
                    </xsl:element>
                </xsl:when>
                <xsl:when test="$clearid = ''">
                    <xsl:element name="formatid">
                        <xsl:value-of select="following-sibling::formatid"/>
                    </xsl:element>      
                </xsl:when>
            </xsl:choose>
        </xsl:template> 
    
        <xsl:template match="formatid"/> 
    
        <!-- copy the rest of the message as is -->
        <xsl:template match="*">
            <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates/>
            </xsl:copy>
        </xsl:template>  
    </xsl:stylesheet>
    

    当我使用这个时,一切正常,除了人2.因为它已被删除

1 个答案:

答案 0 :(得分:1)

你不能简单地做:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="formatid[string(../id)]">
    <xsl:copy>
        <xsl:text>****</xsl:text>
        <xsl:value-of select="substring(../id, string-length(../id) - 1)"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="id"/>

</xsl:stylesheet>

加了:

  

可能有一个scaneario,其中没有formatid元素

然后我会这样做:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/Data">
    <xsl:copy>
        <xsl:for-each select="person">
            <xsl:copy>
                <formatid>
                    <xsl:choose>
                        <xsl:when test="string(formatid)">
                            <xsl:value-of select="formatid"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:text>****</xsl:text>
                            <xsl:value-of select="substring(id, string-length(id) - 1)"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </formatid>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
相关问题