简体   繁体   中英

xsl transformation from xml tag with and without namespaces

I tried to trasform an xml file to another xml file with an xslt stylesheet, but when I do a very simple transformation like

<xsl:template match="/">

    <xsl:apply-templates select="//Package" />


</xsl:template>


<xsl:template match="//Package">


    <behaviour>
        <xsl:attribute name="id"><xsl:value-of select="@Id"/></xsl:attribute>
        <xsl:attribute name="name"><xsl:value-of select="@Name" /></xsl:attribute>

        <attributes>
            <author><xsl:value-of select="./PackageHeader/Vendor"/></author>
            <date><xsl:value-of select="./PackageHeader/Created"/></date>
            <description><xsl:value-of select="./PackageHeader/Description"/></description>
            <goal></goal>
            <revision><xsl:value-of select="./RedefinableHeader/Version"/></revision>
        </attributes>

if the tag Package of the source xml file has an attribute, the transformation didn't happening, like if not read the tag Package, meanwhile if the tag hasn't any attribute, the transformation running correctly..

any idea? I do some error that I can't see? For the transformation i use the xsl processor internal at editor EditiX 2008

the part "inherent" to this thread of my xml source file is

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="xpdl2sybel.xsl"?> 
<Package xmlns:xpdl="http://www.wfmc.org/2008/XPDL2.1" xmlns="http://www.wfmc.org/2008/XPDL2.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Id="message_flow_2" Name="Message Flow 2" xsi:schemaLocation="http://www.wfmc.org/2008/XPDL2.1 http://www.yaoqiang.org/schemas/bpmnxpdl_31.xsd">
    <PackageHeader>
        <XPDLVersion>2.1</XPDLVersion>
        <Vendor>Yaoqiang</Vendor>
        <Created>2010-05-14 22:26:55</Created>
        <Description>Message Flow connecting to Flow Objects within two Pools</Description>
    </PackageHeader>
    <RedefinableHeader>
        <Version>0.03</Version>
    </RedefinableHeader>
</Package>

Thanks in advance!

Your input XML has a default namespace declared xmlns="http://www.wfmc.org/2008/XPDL2.1" . This means that all elements that are unprefixed belong to this namespace.

Therefore you should also declare the namespace in your XSLT. Preferable with a prefix, like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wfmc="http://www.wfmc.org/2008/XPDL2.1" exclude-result-prefixes="wfmc">

The exclude-result-prefixes="wfmc" , will not output the namespace into your result XSLT. Now you have the namespace declared and you can select nodes belonging to this namespace with this prefix, like this:

<xsl:apply-templates select="//wfmc:Package" />
<xsl:template match="//wfmc:Package">

Also note that the use of // will go through all elements everytime you use it. To be more efficient write a XPath that will just find the node directly:

<xsl:apply-templates select="wfmc:Package" />
<xsl:template match="wfmc:Package">

Note that it did not start with a / , because you are already on the root withing your template match.

I also recommend you read some information about XSLT and namespaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM