xslt转换xml与xmlns包括

时间:2013-09-27 13:11:09

标签: xml xslt

我正在尝试转换xml:

    <servicioSDD xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" facade="GestionEstadosSolicitudEspecifico" xmlns="http://new.webservice.namespace">
  <entrada xmlns="">
        <IdSubscription>7daa07b5-348c-463b-ab09-d6fb0e25dc53</IdSubscription>
        <BusinessProcessCode>Expediente Digital</BusinessProcessCode>
    <ParamResponses>
            <ParamResponse>
                <Name>TIPODOCUMENTO</Name>
                <ValParam>DOCGEN_PO_ED</ValParam>
            </ParamResponse>
        <ParamResponse>
                <Name>GN_ID</Name>
                <ValParam>KFX001_2105</ValParam>
            </ParamResponse>
        <ParamResponse>
                <Name>NUM_PERSONA_CLI</Name>
                    <ValParam>F8469</ValParam>
            <ValParam>F8470</ValParam>
                    <ValParam>F8471</ValParam>
            </ParamResponse>
        <ParamResponse>
                <Name>GN_VALIDITYDATE</Name>
                <ValParam>9999-12-31</ValParam>
            </ParamResponse>
            <ParamResponse>
                <Name>CONT_PARTENON</Name>
                <ValParam>00490075442115807771</ValParam>
            </ParamResponse>
        <ParamResponse>
                <Name>ID_SOLICITUD</Name>
                <ValParam>00490075000003590200001</ValParam>
            </ParamResponse>
        <ParamResponse>
                <Name>GN_CREATIONDATE</Name>
                <ValParam>2013-5-21</ValParam>
            </ParamResponse>
        </ParamResponses>
  </entrada>
</servicioSDD>

使用xslt:

<?xml version="1.0"  encoding="ISO-8859-1"  ?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes=''


>
<xsl:template match="/">
<entrada >
      <xsl:for-each select="servicioSDD/entrada/ParamResponses/ParamResponse">
            <xsl:if test="Name='TIPODOCUMENTO'">
                <TIPODOCUMENTO><xsl:value-of select="ValParam"/></TIPODOCUMENTO>
            </xsl:if>
            <xsl:if test="Name='GN_ID'">
                <GN_ID><xsl:value-of select="ValParam"/></GN_ID>
            </xsl:if>
            <xsl:if test="Name='CONT_PARTENON'">
                    <xsl:variable name="contrato" select="ValParam"/>
                <CONT_PARTENON>
                    <CENTRO>
                        <EMPRESA><xsl:value-of select="substring($contrato,1,4)"/></EMPRESA>
                        <CENTRO><xsl:value-of select="substring($contrato,5,4)"/></CENTRO>
                    </CENTRO>
                    <PRODUCTO><xsl:value-of select="substring($contrato,9,3)"/></PRODUCTO>
                    <NUMERO_DE_CONTRATO><xsl:value-of select="substring($contrato,12,7)"/></NUMERO_DE_CONTRATO>
                </CONT_PARTENON>
            </xsl:if>
            <xsl:if test="Name='ID_SOLICITUD'">
                <xsl:variable name="solicitud" select="ValParam"/>
                <ID_SOLICITUD>
                    <CODIGO_DE_SOLICITUD><xsl:value-of select="substring($solicitud,21)"/></CODIGO_DE_SOLICITUD>
                    <ID_CASO_PN>
                        <CODIGO_DE_CASO><xsl:value-of select="substring($solicitud,9,10)"/></CODIGO_DE_CASO>
                        <CENTRO>
                            <EMPRESA><xsl:value-of select="substring($solicitud,1,4)"/></EMPRESA>
                            <CENTRO><xsl:value-of select="substring($solicitud,5,4)"/></CENTRO>
                        </CENTRO>
                        <VERSION><xsl:value-of select="substring($solicitud,19,2)"/></VERSION>
                    </ID_CASO_PN>
                </ID_SOLICITUD>
            </xsl:if>
            <xsl:if test="Name='GN_CREATIONDATE'">
                <xsl:variable name="Cdate" select="ValParam"/>
                <GN_CREATIONDATE><xsl:value-of select="concat(substring($Cdate,9,2),concat(concat('-',substring($Cdate,6,2)), concat('-',substring($Cdate,1,4))))"/></GN_CREATIONDATE>
            </xsl:if>
            <xsl:if test="Name='GN_VALIDITYDATE'">
                <xsl:variable name="Vdate" select="ValParam"/>
                <GN_VALIDITYDATE><xsl:value-of select="concat(substring($Vdate,9,2),concat(concat('-',substring($Vdate,6,2)), concat('-',substring($Vdate,1,4))))"/></GN_VALIDITYDATE>
            </xsl:if>
      </xsl:for-each>
      <xsl:for-each select="servicioSDD/entrada/ParamResponses/ParamResponse[Name='NUM_PERSONA_CLI']">
            <listaPersonaCliente>
                <xsl:for-each select="ValParam">
                    <NUM_PERSONA_CLI>
                        <xsl:variable name='cliente' select="."/>
                        <CODIGO_DE_PERSONA><xsl:value-of select="substring($cliente,2)"/></CODIGO_DE_PERSONA>
                        <TIPO_DE_PERSONA><xsl:value-of select="substring($cliente,1,1)"/></TIPO_DE_PERSONA>
                    </NUM_PERSONA_CLI>
                </xsl:for-each>
            </listaPersonaCliente>
     </xsl:for-each>   
</entrada>
</xsl:template>
</xsl:stylesheet>

我得到的结果是:

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

如果我删除xml:转换完成没有问题,但我无法控制xml格式的到来为我的服务

非常感谢

1 个答案:

答案 0 :(得分:0)

问题出在您的XML中,根元素 servicioSDD 属于命名空间

<servicioSDD ... xmlns="http://new.webservice.namespace">

但是,在您的XSLT中,您可以执行此操作

<xsl:for-each select="servicioSDD/entrada/ParamResponses/ParamResponse">

这意味着它正在寻找一个不在任何名称空间中的 servicioSDD 元素,因此它与XML中的名称空间不匹配。

您可以做的是更改XSLT以声明命名空间,如下所示:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sdd="http://new.webservice.namespace"
exclude-result-prefixes='sdd'>

然后您可以将 xsl:for-each 循环更改为如下

<xsl:for-each select="sdd:servicioSDD/entrada/ParamResponses/ParamResponse">

在这种情况下,您只需要将命名空间前缀添加到 servicioSDD 元素,因为它下面的所有元素都不在命名空间中,因此匹配。

或者,如果不想在XSLT中定义命名空间(也许它可能因XML而异?),那么你可以将 xsl:for-each 更改为this,这将是匹配所有根元素

<xsl:for-each select="*/entrada/ParamResponses/ParamResponse">

或者如果仍想检查它是 servicioSDD 元素,您可以添加条件

<xsl:for-each select="*[local-name()='servicioSDD']/entrada/ParamResponses/ParamResponse">