使用XSL的XML到CSV的特殊字符

时间:2015-11-28 22:32:52

标签: xml csv xslt output

我有一个XML文档,我需要使用XSL将其传递给CSV。 我设法执行了这个过程,但我遇到了特殊字符的问题,即“& quot;”,& amp; ......我需要你进行更改,这些字符不会被更改并以相同的方式保持表示。 我有以下内容:

XML文件

<?xml version="1.0" encoding="UTF-8"?>
<pwlist>
<pwentry>
    <group>General</group>
    <title>Sample Entry</title>
    <username>Greg</username>
    <url>http://www.web.com</url>
    <password>sVoVd2HohmC7hpKYV5Bs</password>
    <notes>This entry is stored in the &#39;General&#39; group.</notes>
    <uuid>4d9a9420ac7c4a8ae688762eac8871a9</uuid>
    <image>0</image>
    <creationtime>2006-12-31T11:52:01</creationtime>
    <lastmodtime>2006-12-31T11:52:01</lastmodtime>
    <lastaccesstime>2006-12-31T11:52:01</lastaccesstime>
    <expiretime expires="false">2999-12-28T23:59:59</expiretime>
</pwentry>
<pwentry>
    <group tree="General">Windows</group>
    <title>Sample Entry #2</title>
    <username>michael@web.com</username>
    <url>http://www.web.com</url>
    <password>L2shNNvQLmOWug68Rz2V</password>
    <notes>This entry is stored in the &#39;Windows&#39; subgroup of the &#39;General&#39; group.</notes>
    <uuid>bddda53fad29037bba34e7e42923c676</uuid>
    <image>0</image>
    <creationtime>2006-12-31T11:52:38</creationtime>
    <lastmodtime>2006-12-31T11:52:38</lastmodtime>
    <lastaccesstime>2006-12-31T11:52:38</lastaccesstime>
    <expiretime expires="false">2999-12-28T23:59:59</expiretime>
</pwentry>
<pwentry>
    <group tree="General">Windows</group>
    <title>Expiring Entry</title>
    <username>me@website.org</username>
    <url>http://www.website.org</url>
    <password>USYbBOdTjaerrN/3l0VK</password>
    <notes>This entry expires on 28.12.2008, at 23:59:59.</notes>
    <uuid>c2674112d67f2787e822d845cde52858</uuid>
    <image>0</image>
    <creationtime>2006-12-31T11:53:38</creationtime>
    <lastmodtime>2006-12-31T11:53:38</lastmodtime>
    <lastaccesstime>2006-12-31T11:53:38</lastaccesstime>
    <expiretime expires="true">2008-12-28T23:59:59</expiretime>
</pwentry>
<pwentry>
    <group tree="General">Windows</group>
    <title>Special Characters Test</title>
    <username>!&quot;§$%&amp;/()=?`´²³{[]}\</username>
    <url></url>
    <password>öüäÖÜÄß&lt;&gt;@€µ®“«</password>
    <notes>The user name and password fields contain special characters.</notes>
    <uuid>6a4aae3134cb7b7d550d0bb7c98bc203</uuid>
    <image>34</image>
    <creationtime>2006-12-31T11:55:57</creationtime>
    <lastmodtime>2006-12-31T11:56:06</lastmodtime>
    <lastaccesstime>2006-12-31T11:56:06</lastaccesstime>
    <expiretime expires="true">2008-12-28T23:59:59</expiretime>
</pwentry>
<pwentry>
    <group tree="General">Windows</group>
    <title>Multi-Line Test</title>
    <username>user@website.com</username>
    <url>http://www.website.com</url>
    <password>v9ffIWkd/jPw/GPLFViW</password>
    <notes>This is a multi-line comment.
This is a multi-line comment.
This is a multi-line comment.
This is a multi-line comment.
This is a multi-line comment.
This is a multi-line comment.</notes>
    <uuid>3d4fe2dc30d4ef0b62da6c7bf85e04ba</uuid>
    <image>0</image>
    <creationtime>2006-12-31T11:56:49</creationtime>
    <lastmodtime>2006-12-31T11:56:49</lastmodtime>
    <lastaccesstime>2006-12-31T11:56:49</lastaccesstime>
    <expiretime expires="false">2999-12-28T23:59:59</expiretime>
</pwentry>
</pwlist>

XSL文件:

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

    <xsl:variable name="delimiter" select="','" />

    <xsl:variable name="fieldArray" >
        <field>group</field>
        <field>title</field>
        <field>username</field>
        <field>url</field>
        <field>password</field>
        <field>notes</field>
        <field>uuid</field>
        <field>image</field>
        <field>creationtime</field>
        <field>lastmodtime</field>
        <field>lastaccesstime</field>
        <field>expiretime</field>
    </xsl:variable>
    <xsl:param name="fields" select="document('')/*/xsl:variable[@name='fieldArray']/*"/>

    <xsl:template match="/">

        <xsl:for-each select="$fields">
            <xsl:if test="position() !=1">
                <xsl:value-of select="$delimiter"/>
            </xsl:if>
            <xsl:value-of select="." />
        </xsl:for-each>

        <xsl:text>&#xa;</xsl:text>

        <xsl:apply-templates select="pwlist/pwentry"/>
    </xsl:template>

    <xsl:template match="pwentry">
        <xsl:variable name="currNode" select="." />

        <xsl:for-each select="$fields">
            <xsl:if test="position() !=1">
                <xsl:value-of select="$delimiter"/>
            </xsl:if>
        <xsl:text>"</xsl:text>
            <xsl:value-of select="$currNode/*[name() = current()]" />
        <xsl:text>"</xsl:text>
    </xsl:for-each>

    <xsl:text>&#xa;</xsl:text>
    </xsl:template>
</xsl:stylesheet>

我尝试过disable-output-escaping =“no”但是没有用,有人能帮帮我吗?谢谢!

0 个答案:

没有答案