如何应用需要名称空间的xsl转换而不输出名称空间?

时间:2019-09-16 10:21:39

标签: xml xslt xml-namespaces

我正在尝试生成以下XML输出,但是当我运行下面的包含名称空间的转换时,是否可以更改此行为?

所需的输出:

<val>Running</val>

当前输出:

<val xmlns:x="http://www.mesa.org/xml/B2MML-V0600">Running</val>

SourceXML:

<?xml version="1.0" encoding="utf-8"?>
<WorkPerformance xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.mesa.org/xml/B2MML-V0600">
<PublishedDate>2019-09-13T16:45:16.4042764+01:00</PublishedDate>
<WorkResponse>
    <JobResponse>
        <JobOrderID />
        <StartTime>2019-09-13T16:45:16.3762939+01:00</StartTime>
        <EquipmentActual>
            <EquipmentID>unaccounted</EquipmentID>
            <Description>0</Description>
            <EquipmentActualProperty>
                <ID>EquipmentState</ID>
                <Value>
                    <ValueString>Running</ValueString>
                    <Key />
                </Value>
            </EquipmentActualProperty>
        </EquipmentActual>
    </JobResponse>
</WorkResponse>
</WorkPerformance>

XSL:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="http://www.mesa.org/xml/B2MML-V0600">
    <xsl:output omit-xml-declaration="yes"  method="xml"/>
    <xsl:template match="/">
        <val><xsl:value-of select="//x:ValueString"/></val>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

我刚刚发现解决方案是将以下内容添加到xsl:

exclude-result-prefixes="x"

因此完整的修订样式表如下:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="http://www.mesa.org/xml/B2MML-V0600"exclude-result-prefixes="x">
<xsl:output omit-xml-declaration="yes"  method="xml"/>
    <xsl:template match="/">
        <val><xsl:value-of select="//x:ValueString"/></val>
    </xsl:template>
</xsl:stylesheet>