【问题标题】:use xsl modify property value inside xml tag使用 xsl 修改 xml 标签内的属性值
【发布时间】:2017-06-23 15:10:07
【问题描述】:

说这是我的 xml

<?xml version="1.0" encoding="UTF-8"?>
<node>
    <file name="abc.txt.bak">
        <fileid value="112358"/>
    </file>
    <location value="Baker Street"/>
</node>

我想使用 xsl 通过删除文件名的 .bak 来转换此 xml。 预期的结果是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<node>
     <file name="abc.txt">
         <fileid value="112358"/>
     </file>
     <location value="Baker Street"/>
</node>

我的xsl文件是这样的,不行。它只是复制所有内容而不更改值。

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

   <xsl:template match="node/file/@name" >
        <xsl:copy>
            <xsl:attribute name="name">
                <xsl:value-of select="substring-before(., '.bak')" />
            </xsl:attribute>
        </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

【问题讨论】:

标签: xml xslt


【解决方案1】:

我已经解决了我自己的问题。只是为了分享。

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="node/file">

        <xsl:variable name="bak_file_name">
            <xsl:value-of select="@name" />
        </xsl:variable>

        <xsl:copy>
            <xsl:apply-templates select="@*"/> 
                <xsl:attribute name="name">
                            <xsl:value-of select="substring-before($bak_file_name, '.bak')" />
                </xsl:attribute>
            <xsl:apply-templates select="fileid"/> 
        </xsl:copy>


    </xsl:template>



</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多