xml到xml转换-尝试合并两个XSL

时间:2019-05-21 05:53:49

标签: xslt

我正在尝试使用xsl将一个xml转换为另一个。

  1. 将输入XML的“名称”属性值从“代码”更改为“ id”
  2. 将输入XML的“名称”属性值从“名称”更改为“名称”

  3. 在json:object下的outpur xml中将json:array下的所有节点复制为 显示在输出xml中

使用两个单独的XSL并尝试确定如何合并这两个XSL,我就能实现我所需要的东西,请检查一下并为我提供帮助

xsl1:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:dp="http://www.datapower.com/extensions" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:date="http://exslt.org/dates-and-times" extension-element-prefixes="dp date">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@name[.='Name']">
        <xsl:attribute name="name"><xsl:value-of select="'name'"/></xsl:attribute>
    </xsl:template>
    <xsl:template match="@name[.='Code']">
        <xsl:attribute name="name"><xsl:value-of select="'id'"/></xsl:attribute>
    </xsl:template>     
    <xsl:template match="@name[.='SimpleCarrier']">
        <xsl:variable name="carrierType">
            <xsl:value-of select="'airlines'"/>
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="$carrierType='airlines'">
                <xsl:attribute name="name"><xsl:value-of select="'airlines'"/></xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
                <xsl:attribute name="name"><xsl:value-of select="'SimpleCarrierNotUpdated'"/></xsl:attribute>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template> 
</xsl:stylesheet>

xsl2:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:dp="http://www.datapower.com/extensions" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:date="http://exslt.org/dates-and-times" extension-element-prefixes="dp date">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>      
    <xsl:template match="/">
        <xsl:copy-of select="//json:array"/>
    </xsl:template>     
</xsl:stylesheet>

输入XML:

<json:object xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
    <json:object name="Header">
        <json:string name="Action">http://webs.abcd.com/CService</json:string>
        <json:string name="RelatesTo">urn:uuid:9455ee68-bc4d-4e6a-9174-fb2000c18e24</json:string>
    </json:object>
    <json:object name="Body">
        <json:object name="GetSimpleCLResponse">
            <json:object name="GetSimpleCLResult">
                <json:array name="SimpleCarrier">
                    <json:object>
                        <json:string name="Code">m9</json:string>
                        <json:string name="Name">1B9FHQK</json:string>
                    </json:object>
                    <json:object>
                        <json:string name="Code">25</json:string>
                        <json:string name="Name">1TIME</json:string>
                    </json:object>                  
                </json:array>
            </json:object>
        </json:object>
    </json:object>
</json:object>

输出XML:

<json:object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd">
    <json:array name="airlines">
        <json:object>
            <json:string name="id">m9</json:string>
            <json:string name="name">1B9FHQK</json:string>
        </json:object>      
        <json:object>
            <json:number name="id">25</json:number>
            <json:string name="name">1TIME</json:string>
        </json:object>      
    </json:array>
</json:object>

1 个答案:

答案 0 :(得分:1)

AFAICT,您所需要做的就是添加:

<xsl:template match="/">
    <xsl:apply-templates select="//json:array"/>
</xsl:template>

到第一个样式表。