XSLT(XML到XML转换)

问题描述 投票:0回答:1

我想转换此XML:

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<advisory>
    <employee_assigned>
        <name>
            Ethel Pinili
        </name>
        <employee_id>
            123221
        </employee_id>
        <group>
            BCMS
        </group>
    </employee_assigned>

    <advised_date>
        <month>April</month>
        <day>13</day>
        <year>2020</year>
    </advised_date>
    <event_notification>
        <incident_date>
            <month>April</month>
            <day>13</day>
            <year>2020</year>
        </incident_date>
        <incident_cause>Theft</incident_cause>
        <incident_nature>Business Interruption</incident_nature>
        <site_affected>
            <site_name>BGC Office</site_name>
            <site_id>BGC221</site_id>
            <site_description>Corporate Office</site_description>
            <site_address>Taguig City, Metro Manila</site_address>
            <site_manager>
                <name>
                    Joel Hagibis
                </name>
                <contact>
                    09192267786
                </contact>
            </site_manager>
            <site_down_time>2 days</site_down_time>

            <affected_subs>128000</affected_subs>
            <damaged>
                <item>
                    <name>Building Power Supply</name>
                    <qty>1</qty>
                    <price>25000</price>
                </item>
                <item>
                    <name>Wirings</name>
                    <qty>100</qty>
                    <price>10000</price>
                </item>
            </damaged>
        </site_affected>
        <incident_description>
            BGC Office is temporarily closed because of electrical malfunctioning. First degree alarm of fire was raised.
        </incident_description>
        <support_documents>Photographs, Security Invistigation Report</support_documents>
        <third_parties>Meralco & MA Electrical Insurance</third_parties>
        <restoration_strategy>Electrical system restoration</restoration_strategy>
    </event_notification>
</advisory>

进入此:

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->


<INR>
    <prepared_by id_no="123221">
        <name>
            Ethel Pinili
        </name>
        <group>
            BCMS
        </group>
    </prepared_by>
    <inr_filing_date>
        <month>April</month>
        <day>13</day>
        <year>2020</year>
    </inr_filing_date>

    <incident>
        <inc_date>
            <month>April</month>
            <day>13</day>
            <year>2020</year>
        </inc_date>
        <cause>Theft</cause>
        <nature>Business Interruption</nature>
        <site>
            <name>BGC Office</name>
            <pla_id>BGC221</pla_id>
            <description>Corporate Office</description>
            <place_of_incident>Taguig City, Metro Manila</place_of_incident>
            <manager contact="09192267786">Joel Hagibis</manager>
            <down_time>2 days</down_time>
            <affected_subs>128000</affected_subs>
            <damaged>
                <item>
                    <name>Building Power Supply</name>
                    <qty>1</qty>
                    <price>25000</price>
                </item>
                <item>
                    <name>Wirings</name>
                    <qty>100</qty>
                    <price>10000</price>
                </item>
            </damaged>
        </site>
        <incident_description>
            BGC Office is temporarily closed because of electrical malfunctioning.
        </incident_description>
        <support_doc>Photographs, Security Invistigation Report</support_doc>
        <third_parties>Meralco & MA Electrical Insurance</third_parties>
        <restoration_strategy>Electrical system restoration</restoration_strategy>
    </incident>
</INR>

必须使用XSL样式表。我所有的尝试都出现一个错误,指出

“实体名称必须紧跟实体中的'&'参考。com.sun.org.apache.xml.internal.utils.WrappedRuntimeException:实体名称必须紧跟在实体引用中的“&”之后。”]

到目前为止是我的尝试:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <inr>
            <prepared_by>
                <name>
                    <xsl:value-of select="employee_assigned/name/text()"/>
                </name>
                <group>
                    <xsl:value-of select="employee_assigned/group/text()"/>
                </group>
            </prepared_by>
            <inr_filing_date>
                <month>
                    <xsl:value-of select="../advised_date/month/text()"/>
                </month>
                <day>
                    <xsl:value-of select="../advised_date/day/text()"/>
                </day>
                <year>
                    <xsl:value-of select="../advised_date/day/text()"/>
                </year>
            </inr_filing_date>

        </inr>
    </xsl:template>
</xsl:stylesheet>
xml xslt transformation xsl-stylesheet
1个回答
0
投票

您的输入在第66行包含未转义的与号:

    <third_parties>Meralco & MA Electrical Insurance</third_parties>

&号是保留字符,必须转义:

    <third_parties>Meralco &amp; MA Electrical Insurance</third_parties>

按原样,您的输入不是格式正确的XML文档,因此XSLT无法对其进行处理。

© www.soinside.com 2019 - 2024. All rights reserved.