如何使用xslt将子节点及其属性移动到父节点

时间:2014-01-09 11:05:08

标签: xml xslt xslt-1.0

我有以下xml

<PQIssueInquiry>
               <PQIssueslist>
              <transaction id="2013-04-29-05.03.15.644700T01">
                <id>2013-04-29-05.03.15.644700T01</id>
                <priority>999</priority>
                <issueID>20130429-152405-DT73606</issueID>
                <subject>Member</subject>
                <issueCategory>MS Care1st Member</issueCategory>
                <issueSubCategory>Demographic Change</issueSubCategory>
                <providerNumber>PCP4</providerNumber>
                <memberNumber>10020*03</memberNumber>
                <businessArea>CSMHCQA</businessArea>
                <type>ISSUE</type>
                <status>CATCHALL</status>
                <groupNumber>COMM*H</groupNumber>
                <lockStatus>ASSIGNED</lockStatus>
                <assignID>DT73606</assignID>
                <queueCode>CATCHALL</queueCode>
                <lockworkFlag>N</lockworkFlag>
                <origUserID>DT73606</origUserID>
                <CFLG>2</CFLG>
              </transaction>

            </PQIssueslist>
          </PQIssueInquiry>

现在,我想从上面的xml中删除节点PQIssueslist,xml输出应该如下所示:

<PQIssueInquiry>

            <transaction id="2013-04-29-05.03.15.644700T01">
            <id>2013-04-29-05.03.15.644700T01</id>
            <priority>999</priority>
            <issueID>20130429-152405-DT73606</issueID>
            <subject>Member</subject>
            <issueCategory>MS Care1st Member</issueCategory>
            <issueSubCategory>Demographic Change</issueSubCategory>
            <providerNumber>PCP4</providerNumber>
            <memberNumber>10020*03</memberNumber>
            <businessArea>CSMHCQA</businessArea>
            <type>ISSUE</type>
            <status>CATCHALL</status>
            <groupNumber>COMM*H</groupNumber>
            <lockStatus>ASSIGNED</lockStatus>
            <assignID>DT73606</assignID>
            <queueCode>CATCHALL</queueCode>
            <lockworkFlag>N</lockworkFlag>
            <origUserID>DT73606</origUserID>
            <CFLG>2</CFLG>
          </transaction>


      </PQIssueInquiry>

我已经做了类似下面的操作来删除指定的节点,但我得到的例外是“根元素之后的文档中的标记必须格式正确。”

我使用的代码是:

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

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="PQIssueslist">

        <xsl:apply-templates select="node()"/>

    </xsl:template>

</xsl:stylesheet>

帮助表示赞赏。我有两个xsl文件,其中第一个输出xml是第二个xsl的输入。

第一个xsl文件:

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

<xsl:variable name="spName" select="//PQ/PQIssueInquiry/storedProcedureName"/>
    <xsl:template match="/">
        <PQIssueslist>
             <xsl:apply-templates select="//results/row" />
         </PQIssueslist>
   </xsl:template>

    <xsl:template match="//results/row">
      <transaction>
              <xsl:attribute name="id"><xsl:value-of select=".//IKEY"/></xsl:attribute>
                <id><xsl:value-of select=".//IKEY"/></id>         
                <priority><xsl:value-of select=".//PRTY"/></priority>
                <issueID><xsl:value-of select=".//ISSUEID"/></issueID>
                <subject><xsl:value-of select=".//SUBJECT"/></subject>
                <issueCategory><xsl:value-of select=".//ISSTYP"/></issueCategory>
                <issueSubCategory><xsl:value-of select=".//ISSCAT1"/></issueSubCategory>
                <providerNumber><xsl:value-of select=".//RELPRV"/></providerNumber>
                <memberNumber><xsl:value-of select=".//RELMEM"/></memberNumber>
                <businessArea><xsl:value-of select=".//UNITCD"/></businessArea>
                <type><xsl:value-of select=".//WRKTYPE"/></type>
                <status><xsl:value-of select=".//STATCD"/></status>
                <groupNumber><xsl:value-of select=".//RELGRP"/></groupNumber>
                <lockStatus><xsl:value-of select=".//LOCKSTAT"/></lockStatus>
                <assignID><xsl:value-of select=".//ASSIGNID"/></assignID>
                <queueCode><xsl:value-of select=".//QUEUECD"/></queueCode>
                <lockworkFlag><xsl:value-of select=".//LOCKWORK_FLG"/></lockworkFlag>
                <origUserID><xsl:value-of select=".//ORIGUSERID"/></origUserID>
        <CFLG><xsl:value-of select=".//INXFLD01"/></CFLG>
        <xsl:if test="$spName = 'PQOCLKP' ">
                <callerType>Other</callerType>
                <contactName><xsl:value-of select=".//CLSTNAME"/>,&#160;<xsl:value-of select=".//CFSTNAME"/></contactName>
                <contactPhone><xsl:value-of select=".//CPHONE"/></contactPhone>
                <contactOrganization><xsl:value-of select=".//CORGNAM"/></contactOrganization>
        </xsl:if>
        </transaction>
           <xsl:if test="$spName != 'PQOCLKP' ">
               <callerType>NotOther</callerType>
           </xsl:if>
            <sort>
                <field><xsl:value-of select=".//FIELD"/></field>
                <ascending><xsl:value-of select=".//ASCENDING"/></ascending>
            </sort>
  </xsl:template>
</xsl:stylesheet>

第二个xsl文件是:

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

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>

    </xsl:template>

    <xsl:template match="PQIssueslist">
    <xsl:apply-templates select="node()"/>
    </xsl:template>

</xsl:stylesheet>

第一个XSL文件的示例输入是:

- <ABC>
- <PQIssueInquiry>
  <userID>DT11</userID> 
  <password>ddd</password> 
  <hostAddress>12.34.54</hostAddress> 
  <hostPort>123</hostPort> 
  <storedProcedureName>PQUSERISSLOOKUP</storedProcedureName> 
  <userName>ABC</userName> 
  <fromDate>2013-04-12-00.00.00.000000</fromDate> 
  <toDate>2013-05-11-23.59.59.999999</toDate> 
  </PQIssueInquiry>
- <AWDSRVRunStoredProcedure xmlns:lxslt="http://xml.apache.org/xslt">
  <hostAddress>10.193.236.57</hostAddress> 
  <hostPort>12955</hostPort> 
  <timeout>120000</timeout> 
  <trace>0</trace> 
  <readable>N</readable> 
  <userID>DT77214</userID> 
- <storedProcedure id="DUMMY">
- <row>
  <ISSTYP>MS Care1st Member</ISSTYP> 
  <ISSCAT1>Demographic Change</ISSCAT1> 
  <UNITCD>CSMHCQA</UNITCD> 
  <ORIGUSERID>DT73606</ORIGUSERID> 
  <PRTY>999</PRTY> 
  <WRKTYPE>ISSUE</WRKTYPE> 
  <ASSIGNID>DT73606</ASSIGNID> 
  <RELGRP>COMM*H</RELGRP> 
  <INXFLD01>2</INXFLD01> 
  <SUBJECT>Member</SUBJECT> 
  <QUEUECD>CATCHALL</QUEUECD> 
  <ISSUEID>20130429-152405-DT73606</ISSUEID> 
  <STATCD>CATCHALL</STATCD> 
  <IKEY>2013-04-29-05.03.15.644700T01</IKEY> 
  <RELPRV>PCP4</RELPRV> 
  <RELMEM>10020*03</RELMEM> 
  <LOCKWORK_FLG>N</LOCKWORK_FLG> 
  <LOCKSTAT>ASSIGNED</LOCKSTAT> 
  </row>
-
  </list>
  </ABC>

1 个答案:

答案 0 :(得分:1)

在输入示例的末尾有</PQIssueInquiry>,这就是问题,与任何XSLT无关。 XSLT很好,但您需要提供格式良好的XML输入样本。