使用XSLT代码从xml创建XML

时间:2017-09-15 14:58:24

标签: xml xslt xpath xsd

    <MemoPad>
       <ENTRYTYPE>ReviewFees</ENTRYTYPE>
       <ENTRYDATETIME>2017-09-05</ENTRYDATETIME>
       <USERID>admin</USERID>
      <MEMOENTRY>Review fees test</MEMOENTRY>
    </MemoPad>
    <MemoPad>
       <ENTRYTYPE>UnderwriterReview</ENTRYTYPE>
        <ENTRYDATETIME>2017-04-12</ENTRYDATETIME>
         <USERID>ADMIN</USERID>
         <MEMOENTRY>Underwriter Rule</MEMOENTRY>
    </MemoPad>
     <MemoPad>
          <ENTRYTYPE>UnderwriterReview</ENTRYTYPE>
          <ENTRYDATETIME>2017-09-05</ENTRYDATETIME>
          <USERID>admin</USERID>
          <MEMOENTRY>month bank statement. </MEMOENTRY>
     </MemoPad>

我为上面的部分尝试了很多写XSLT但是无法破解它。我正在寻找的基本上我想迭代每个memopad项目,并在memopad中为每个EntryType我想创建一个像

的xml
      <ReviewFeesType>
           <ReviewFee>               
                <ENTRYDATETIME>2017-09-05</ENTRYDATETIME>
                <USERID>admin</USERID>
                <MEMOENTRY>Review fees test</MEMOENTRY>
           </ReviewFee>
       </ReviewFeesType>

但是现在对于多种类型的条目类型应该如下创建。

     <UnderwriterReviewType>
        <UnderwriterReview>
            <ENTRYDATETIME>2017-04-12</ENTRYDATETIME>
            <USERID>ADMIN</USERID>
            <MEMOENTRY>Underwriter Rule</MEMOENTRY>
        </UnderwriterReview>
         <UnderwriterReview>
             <ENTRYDATETIME>2017-09-05</ENTRYDATETIME>
             <USERID>admin</USERID>
             <MEMOENTRY>month bank statement. </MEMOENTRY>
           </UnderwriterReview>
     </UnderwriterReviewType>  

我如何为此编写XSLT? 请帮忙。

我试过的是

   <MemoPads>
      <xsl:for-each select="MemoPad">            
        <xsl:element name="{ENTRYTYPE}">
          <ENTRYDATETIME>
            <xsl:value-of select="ENTRYDATETIME"/>
          </ENTRYDATETIME>
          <USERID>
            <xsl:value-of select="USERID"/>
          </USERID>
          <MEMOENTRY>
            <xsl:value-of select="MEMOENTRY"/>
          </MEMOENTRY>
        </xsl:element>            
      </xsl:for-each>
    </MemoPads>

提前谢谢你。

1 个答案:

答案 0 :(得分:0)

这是一个2147483647示例。如果您使用grouping,则需要声明XSLT 1.0

<xsl:key>

提供的输入XML格式不正确,因为它不包含<xsl:key name="kEntryType" match="MemoPad" use="ENTRYTYPE" /> 节点,因此我假设root<MemoPads>节点。下面的XSLT将有助于获得所需的输出。评论应该有助于理解逻辑。

XSLT 1.0

root

<强>输出

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*"/>

    <!-- declare a grouping key -->
    <xsl:key name="kEntryType" match="MemoPad" use="ENTRYTYPE" />

    <xsl:template match="/">
        <!-- create root node in output -->
        <MemoPads>
            <!-- loop for all the groups based on the declared key -->
            <xsl:for-each select="//MemoPad[generate-id() = generate-id(key('kEntryType', ENTRYTYPE)[1])]">
                <!-- create parent element for the grouped elements -->
                <xsl:element name="{concat(ENTRYTYPE,'Type')}">
                    <!-- loop through the set of elements within the key -->
                    <xsl:for-each select="key('kEntryType', ENTRYTYPE)">
                        <!-- create element as required in the output -->
                        <xsl:element name="{ENTRYTYPE}">
                            <!-- copy all elements except the ENTRYTYPE -->
                            <xsl:copy-of select="*[not(self::ENTRYTYPE)]" />
                        </xsl:element>
                    </xsl:for-each>
                </xsl:element>
            </xsl:for-each>
        </MemoPads>
    </xsl:template>
</xsl:stylesheet>