将String(以xml的形式)转换为XML元素

时间:2017-02-13 16:22:01

标签: xslt

我的源XSD for XSL转换:

<?xml version= '1.0' encoding= 'UTF-8' ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://www.quo.com"
            targetNamespace="http://www.quo.com"
            elementFormDefault="qualified"
            xmlns:emc="http://www.quo.com"
<xsd:complexType name="HeaderType">
  <xsd:sequence>
   <xsd:element name="messageId" type="xsd:string"/>
   <xsd:element name="messageType" type="xsd:string"/>
  </xsd:sequence>
  <xsd:attribute name="version" type="xsd:int"/>
</xsd:complexType>   
<xsd:complexType name="ResponseType">
  <xsd:sequence>
   <xsd:element name="errorCode" type="xsd:string"/>
   <xsd:element name="errorString" type="xsd:string"/>
  </xsd:sequence>
</xsd:complexType> 
<xsd:element name="AckMessage">
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element name="header" type="HeaderType"/> 
    <xsd:element name="payload" type="xsd:string"/> 
   </xsd:sequence>
  </xsd:complexType>
</xsd:element>
<xsd:element name="response" type="ResponseType"/>  
</xsd:schema>

我的源XSD示例XML是:

<AckMessage>
<header  version="1">
 <messageId>messageId1</messageId>
 <messageType>sourceSystemId2</messageType>
</header>
 <payload>
  <bookstore>
   <book>
    <title  lang="en">Harry Potter</title>
    <author>J K Rowling</author>
    <year>2005</year>
    <price>29.99</price>
   </book>
   <book>
    <title  lang="en">Harry Potter 1</title>
    <author>J K Rowling</author>
    <year>2006</year>
    <price>29.99</price>
   </book>
  </bookstore>
 </payload>
</AckMessage>

源XSD中的payload元素是字符串类型,我在其中获取根元素书店的整个XML片段

My Target XSD for XSL转换:

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://www.quo.com/ns/bookstore"
            targetNamespace="http://www.quo.com/ns/bookstore"
            elementFormDefault="qualified">
  <xsd:element name="bookstore">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="book" maxOccurs="unbounded">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="title">
                <xsd:complexType>
                  <xsd:simpleContent>
                    <xsd:extension base="xsd:string">
                      <xsd:attribute name="lang" type="xsd:string"/>
                    </xsd:extension>
                  </xsd:simpleContent>
                </xsd:complexType>
              </xsd:element>
              <xsd:element name="author" type="xsd:string"/>
              <xsd:element name="year" type="xsd:integer"/>
              <xsd:element name="price" type="xsd:float"/>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

我想将Payload元素从源模式转换为目标模式的bookstore元素。

当我尝试通过简单转换时,我收到的错误无法将简单元素转换为XML元素。

我试过xsl:

  <xsl:variable name="variable1">
    <xsl:value-of select="/msg_in_out:esbAckMessage/msg_in_out:payload"
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>
  </xsl:variable>
  <xsl:template match="/">
    <imp1:bookstore>
          <xsl:value-of select="$variable1"/>
    </imp1:bookstore>
  </xsl:template>
</xsl:stylesheet>

但我得到输出

<?xml version = '1.0' encoding = 'UTF-8'?>
<imp1:bookstore xmlns:imp1="http://www.quo.com/ns/bookstore">


        Harry Potter
        J K Rowling
        2005
        29.99


        Harry Potter 1
        J K Rowling
        2006
        29.99


   </imp1:bookstore>

如何使用Xml标签获取Payload元素内的所有内容?

答案是使用副本

  <xsl:variable name="variable1">
    <xsl:value-of select="/msg_in_out:esbAckMessage/msg_in_out:payload"
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>
  </xsl:variable>
  <xsl:template match="/">
    <imp1:bookstore>
          <xsl:copy-of select="$variable1"/>
    </imp1:bookstore>
  </xsl:template>
</xsl:stylesheet>

0 个答案:

没有答案