无法使用XSLT生成所需的JSON

时间:2014-11-15 07:37:01

标签: xml json xslt

我试图通过使用XSLT从现有XML获取数据来创建JSON格式,但我没有得到所需的输出。

XML INPUT

<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
   <Header>
      <MessageId>{FB6E5FF6-AA89-4E56-8079-AFF7BAB84486}</MessageId>
      <Action>http://tempuri.org/AIFTrialQueryService/find</Action>
   </Header>
   <Body>
      <MessageParts>
         <AIFTrialQuery xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/AIFTrialQuery">
            <SMAStageTable_1 class="entity">
               <StageId>Closed</StageId>
               <StageDescription>Closed</StageDescription>
            </SMAStageTable_1>
            <SMAStageTable_1 class="entity">
               <StageId>In Process</StageId>
               <StageDescription>In Process</StageDescription>
            </SMAStageTable_1>
            <SMAStageTable_1 class="entity">
               <StageId>Open</StageId>
               <StageDescription>Open</StageDescription>
            </SMAStageTable_1>
         </AIFTrialQuery>
      </MessageParts>
   </Body>
</Envelope>

XSLT INPUT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:output method="text" encoding="utf-8" indent="no"/>

  <xsl:template match="/">
     <xsl:apply-templates match="Envelope"/>  
  </xsl:template>           

<xsl:template match="AIFTrialQuery">

 {
            'Items': [
                <xsl:for-each select="SMAStageTable_1">
                    {
                        'status': '<xsl:value-of select="StageId"/>',
                        'statusName': '<xsl:value-of select="StageDescription"/>'

                    },
                </xsl:for-each>
            ]
        }

</xsl:template>

输出我完全不受欢迎的输出

{FB6E5FF6-AA89-4E56-8079-AFF7BAB84486}
http://tempuri.org/AIFTrialQueryService/find
Closed
Closed
In Process
In Process
Open
Open

预期输出

 {
                'Items': [

                        {
                            'status': 'Closed',
                            'statusDescription': 'Closed'

                        },
            {
                            'status': 'Closed',
                            'statusDescription': 'Closed'

                        },

            {
                            'status':  'In Process'
                            'statusDescription':  'In Process'

                        }

                ]
}

1 个答案:

答案 0 :(得分:4)

希望这有帮助

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
            xmlns:myUtils="pda:MyUtils" 
            xmlns:AIF="http://schemas.microsoft.com/dynamics/2008/01/documents/AIFTrialQuery" 
            xmlns:Env="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<xsl:output method="text" encoding="utf-8"/>
<xsl:template match="/">
  <xsl:apply-templates select="Env:Envelope/Env:Body/Env:MessageParts/AIF:AIFTrialQuery"/>
</xsl:template>
<xsl:template match="AIF:AIFTrialQuery">
  {
  'Items': [
   <xsl:for-each select="AIF:SMAStageTable_1">
    {
     'status': '<xsl:value-of select="AIF:StageId"/>',
     'statusName': '<xsl:value-of select="AIF:StageDescription"/>'

    },
   </xsl:for-each>
  ]
 }
</xsl:template>

这在我的最后工作。 我想你必须提到元素节点与xslt解析器相关联的命名空间来定位元素。