创建新 XML 结构时出现 XSLT 代码问题

问题描述 投票:0回答:1

我正在尝试为以下要求编写 XSLT,其中

/parent/queryCompoundEmployeeResponse/CompoundEmployee/person/person_id_external = /parent/row/PERSON_ID_EXTERNAL 

然后将

/parent/row
的内容剪切并粘贴到相应的
CompoundEmployee
下。

此外,

<parent>
标签应从输出中删除,所有
<CompoundEmployee>
应位于
<queryCompoundEmployeeResponse>
下。

我已经尝试过下面提到的代码,它没有按预期返回输出。 请求您的帮助。

输入

<?xml version='1.0' encoding='UTF-8'?>
<parent>
    <queryCompoundEmployeeResponse>
        <CompoundEmployee>
            <person>
                <person_id_external>100007</person_id_external>
            </person>
        </CompoundEmployee>
        <CompoundEmployee>
            <person>
                <person_id_external>100008</person_id_external>
            </person>
        </CompoundEmployee>
    </queryCompoundEmployeeResponse>
    <row>
        <PERSON_ID_EXTERNAL>100008</PERSON_ID_EXTERNAL>
        <APPLICANT_NUMBER>34</APPLICANT_NUMBER>
    </row>
    <row>
        <PERSON_ID_EXTERNAL>100007</PERSON_ID_EXTERNAL>
        <APPLICANT_NUMBER>12</APPLICANT_NUMBER>
    </row>
</parent>

输出:

    <?xml version='1.0' encoding='UTF-8'?>
        <queryCompoundEmployeeResponse>
            <CompoundEmployee>
                <person>
                    <person_id_external>100007</person_id_external>
                </person>
                <row>
                    <PERSON_ID_EXTERNAL>100007</PERSON_ID_EXTERNAL>
                    <APPLICANT_NUMBER>12</APPLICANT_NUMBER>
                </row>
            </CompoundEmployee>
            <CompoundEmployee>
                <person>
                    <person_id_external>100008</person_id_external>
                </person>
            </CompoundEmployee>
                <row>
            <PERSON_ID_EXTERNAL>100008</PERSON_ID_EXTERNAL>
            <APPLICANT_NUMBER>34</APPLICANT_NUMBER>
        </row>
        </queryCompoundEmployeeResponse>

当前代码

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    
    <!-- Identity template copies everything as is -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <!-- Matches rows with PERSON_ID_EXTERNAL and moves them under corresponding CompoundEmployee -->
    <xsl:template match="parent/row">
        <xsl:variable name="currentId" select="PERSON_ID_EXTERNAL"/>
        <xsl:variable name="targetCompoundEmployee" select="//CompoundEmployee[person/person_id_external = $currentId]"/>
        
        <!-- Copy row content under corresponding CompoundEmployee -->
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
        
        <!-- Remove the row after moving it -->
        <xsl:apply-templates select="//row[PERSON_ID_EXTERNAL = $currentId]/following-sibling::row[1]"/>
        
        <!-- Remove empty parent tag if there are no more rows -->
        <xsl:if test="not(//row)">
            <xsl:apply-templates select="parent::node()[not(self::row)]"/>
        </xsl:if>
    </xsl:template>
    
    <!-- Matches parent and moves CompoundEmployee under queryCompoundEmployeeResponse -->
    <xsl:template match="parent">
        <xsl:apply-templates select="queryCompoundEmployeeResponse"/>
    </xsl:template>
    
</xsl:stylesheet>
xml xslt
1个回答
0
投票

您显示的输出与您的描述不符。

我想你想做:

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

<xsl:key name="rows" match="row" use="PERSON_ID_EXTERNAL" />
    
<xsl:template match="/parent">
    <queryCompoundEmployeeResponse>
        <xsl:for-each select="queryCompoundEmployeeResponse/CompoundEmployee">
            <xsl:copy>
                <xsl:copy-of select="person"/>
                <xsl:copy-of select="key('rows', person/person_id_external)"/>
            </xsl:copy>
        </xsl:for-each>
    </queryCompoundEmployeeResponse>
</xsl:template>
    
</xsl:stylesheet>

返回:

<?xml version="1.0" encoding="utf-8"?>
<queryCompoundEmployeeResponse>
   <CompoundEmployee>
      <person>
         <person_id_external>100007</person_id_external>
      </person>
      <row>
         <PERSON_ID_EXTERNAL>100007</PERSON_ID_EXTERNAL>
         <APPLICANT_NUMBER>12</APPLICANT_NUMBER>
      </row>
   </CompoundEmployee>
   <CompoundEmployee>
      <person>
         <person_id_external>100008</person_id_external>
      </person>
      <row>
         <PERSON_ID_EXTERNAL>100008</PERSON_ID_EXTERNAL>
         <APPLICANT_NUMBER>34</APPLICANT_NUMBER>
      </row>
   </CompoundEmployee>
</queryCompoundEmployeeResponse>
© www.soinside.com 2019 - 2024. All rights reserved.