xslt值列表转换

时间:2018-07-19 22:16:43

标签: xml xslt

源系统中有两个值列表,下面列出了这些值(旧)。在目标系统中,存在相同的值列表,但具有一些不同的值。我需要进行XSLT转换,因此,源系统的旧值已正确转换为具有新值的目标。

值列表

01 Secondary_Fraud_Type

旧值

AML
Code of Conduct Breach,
Commission,
Internal,
Misappropriation,
Theft of Funds,
Expenses.

值列表

01 Secondary_Fraud_Type

新值:

AML/Suspicious Transactions,
Code of Conduct Violation,
Commission Scheme,
Internal Theft,
Misappropriation of Funds,
Misappropriation of Funds,
Expenses Travel.

值列表

02 Primary_Allegation_SubType 

旧值:

Investigative Services - Special Inquiry:441 - Special Inquiry,
Investigative Services - General:298 - Locate.

值列表

02 Primary_Allegation_SubType 

新值:

Miscellaneous - Inactive:300 - Verification,
Not Required - Inactive:100 - Closing file,

输入:

<?xml version="1.0" encoding="UTF-16"?>
<Records count="2">
    <Record parentId="0" moduleId="75"
        levelGuid="b0c2d9a1-167c-4fee-ad91-4b4e7b098b4b" levelId="113" contentId="1010597">
        <Field type="6" guid="34c044c6-b841-4c52-b9f3-b7a56ed7dbd4" id="296">123529</Field>
        <Field type="6" guid="ab6c5129-ed5b-470d-b290-9a7d84123ad3" id="28384">1010597</Field>
        <Field type="4" guid="a0d192ea-9767-4c7e-8a56-4ff973141920" id="24284">
            <ListValues>
                <ListValue id="43156" displayName="Code of Conduct Breach">Code of Conduct Breach</ListValue>
                <ListValue id="43159" displayName="Expenses">Expenses</ListValue>
                <ListValue id="43160" displayName="Forgery">Forgery</ListValue>
                <ListValue id="46729" displayName="AML">AML</ListValue>
            </ListValues>
        </Field>
        <Field type="4" guid="52926f70-9a74-4799-8a14-8d25a1b062da" id="23396">
            <ListValues>
                <ListValue id="43085" displayName="Investigative Services - Special Inquiry:441 - Special Inquiry/Annuity">441 - Special Inquiry/Annuity</ListValue>
            </ListValues>
        </Field>
    </Record>
    <Record parentId="0" moduleId="75"
        levelGuid="b0c2d9a1-167c-4fee-ad91-4b4e7b098b4b" levelId="113" contentId="1011440">
        <Field type="6" guid="34c044c6-b841-4c52-b9f3-b7a56ed7dbd4" id="296">123564</Field>
        <Field type="6" guid="ab6c5129-ed5b-470d-b290-9a7d84123ad3" id="28384">1011440</Field>
        <Field type="4" guid="a0d192ea-9767-4c7e-8a56-4ff973141920" id="24284">
            <ListValues>
                <ListValue id="43157" displayName="Commission">Commission</ListValue>
                <ListValue id="43159" displayName="Expenses">Expenses</ListValue>
                <ListValue id="43161" displayName="Identity Theft">Identity Theft</ListValue>
                <ListValue id="43162" displayName="Internal">Internal</ListValue>
                <ListValue id="43163" displayName="Misappropriation">Misappropriation</ListValue>
                <ListValue id="43164" displayName="Misrepresentation">Misrepresentation</ListValue>
            </ListValues>
        </Field>
        <Field type="4" guid="52926f70-9a74-4799-8a14-8d25a1b062da" id="23396">
            <ListValues>
                <ListValue id="43056" displayName="Investigative Services - General:298 - Locate">298 - Locate</ListValue>
            </ListValues>
        </Field>
    </Record>
    <Metadata>
        <FieldDefinitions>
            <FieldDefinition guid="34c044c6-b841-4c52-b9f3-b7a56ed7dbd4" id="296" alias="Incident_ID" name="Incident ID" />
            <FieldDefinition guid="52926f70-9a74-4799-8a14-8d25a1b062da" id="23396" alias="Primary_Allegation_SubType" name="Primary Allegation Sub-Type" />
            <FieldDefinition guid="a0d192ea-9767-4c7e-8a56-4ff973141920" id="24284" alias="Secondary_Fraud_Type" name="Additional Fraud Types" />
            <FieldDefinition guid="ab6c5129-ed5b-470d-b290-9a7d84123ad3" id="28384" alias="Unique_Tracking_ID" name="Unique Tracking ID-I" />
        </FieldDefinitions>
    </Metadata>
    <LevelCounts>
        <LevelCount count="2" guid="b0c2d9a1-167c-4fee-ad91-4b4e7b098b4b" id="113" />
    </LevelCounts>
</Records>

预期输出:(已替换为新值)

<?xml version="1.0" encoding="UTF-8"?>
<ex:Record xmlns:ex="http://archertech.com/Print/Export">
    <ex:Item>
        <ex:Incident_ID>INC-123529</ex:Incident_ID>
        <ex:Unique_Tracking_ID>1010597</ex:Unique_Tracking_ID>
        <ex:Secondary_Fraud_Type>
            <ex:Item>Code of Conduct Violation</ex:Item>
            <ex:Item>Expenses Travel</ex:Item>
            <ex:Item>Forgery</ex:Item>
            <ex:Item>AML/Suspicious Transactions</ex:Item>
        </ex:Secondary_Fraud_Type>
        <ex:Primary_Allegation_SubType>
            <ex:Item>Miscellaneous - Inactive:300 - Verification</ex:Item>
        </ex:Primary_Allegation_SubType>
    </ex:Item>
    <ex:Item>
        <ex:Incident_ID>INC-123564</ex:Incident_ID>
        <ex:Unique_Tracking_ID>1011440</ex:Unique_Tracking_ID>
        <ex:Secondary_Fraud_Type>
            <ex:Item>Commission Scheme</ex:Item>
            <ex:Item>Expenses Travel</ex:Item>
            <ex:Item>Identity Theft</ex:Item>
            <ex:Item>Internal Theft</ex:Item>
            <ex:Item>Misappropriation of Funds</ex:Item>
            <ex:Item>Misrepresentation</ex:Item>
        </ex:Secondary_Fraud_Type>
        <ex:Primary_Allegation_SubType>
            <ex:Item>Not Required - Inactive:100 - Closing file</ex:Item>
        </ex:Primary_Allegation_SubType>
    </ex:Item>
</ex:Record>

1 个答案:

答案 0 :(得分:0)

解决方案之一是使用模板方法将旧值转换为新值,并将旧节点转换为新节点,例如<Record><ex:Item><ListValue><ex:Item>

<xsl:template match="ListValue[@displayName = 'Code of Conduct Breach']" mode="updateDesc">
    <ex:Item><xsl:text>Code of Conduct Violation</xsl:text></ex:Item>
</xsl:template>

下面是获取输出的完整XSLT。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ex="http://archertech.com/Print/Export">
    <xsl:output method="xml" />
    <xsl:strip-space elements="*" />

    <xsl:template match="Records">
        <ex:Record>
            <xsl:apply-templates />
        </ex:Record>
    </xsl:template>

    <xsl:template match="Record">
        <ex:Item>
            <xsl:apply-templates />
        </ex:Item>
    </xsl:template>

    <!-- Incident_ID -->
    <xsl:template match="Field[@id = '296']">
        <xsl:variable name="elemName" select="concat('ex:', ancestor::Records/Metadata/FieldDefinitions/FieldDefinition[@id = '296']/@alias)" />
        <xsl:element name="{$elemName}">
            <xsl:value-of select="concat('INC-',.)" />
        </xsl:element>
    </xsl:template>

    <!-- Unique_Tracking_ID -->
    <xsl:template match="Field[@id = '28384']">
        <xsl:variable name="elemName" select="concat('ex:', ancestor::Records/Metadata/FieldDefinitions/FieldDefinition[@id = '28384']/@alias)" />
        <xsl:element name="{$elemName}">
            <xsl:value-of select="." />
        </xsl:element>
    </xsl:template>

    <!-- Secondary_Fraud_Type -->
    <xsl:template match="Field[@id = '24284']">
        <xsl:variable name="elemName" select="concat('ex:', ancestor::Records/Metadata/FieldDefinitions/FieldDefinition[@id = '24284']/@alias)" />
        <xsl:element name="{$elemName}">
            <xsl:apply-templates mode="updateDesc" />
        </xsl:element>
    </xsl:template>

    <!-- Primary_Allegation_SubType -->
    <xsl:template match="Field[@id = '23396']">
        <xsl:variable name="elemName" select="concat('ex:', ancestor::Records/Metadata/FieldDefinitions/FieldDefinition[@id = '23396']/@alias)" />
        <xsl:element name="{$elemName}">
            <xsl:apply-templates mode="updateDesc" />
        </xsl:element>
    </xsl:template>

    <!-- ListValue -->
    <xsl:template match="ListValue" mode="updateDesc">
        <ex:Item><xsl:value-of select="." /></ex:Item>
    </xsl:template>
    <xsl:template match="ListValue[@displayName = 'Code of Conduct Breach']" mode="updateDesc">
        <ex:Item><xsl:text>Code of Conduct Violation</xsl:text></ex:Item>
    </xsl:template>
    <xsl:template match="ListValue[@displayName = 'Expenses']" mode="updateDesc">
        <ex:Item><xsl:text>Expenses Travel</xsl:text></ex:Item>
    </xsl:template>
    <xsl:template match="ListValue[@displayName = 'AML']" mode="updateDesc">
        <ex:Item><xsl:text>AML/Suspicious Transactions</xsl:text></ex:Item>
    </xsl:template>
    <xsl:template match="ListValue[@displayName = 'Investigative Services - Special Inquiry:441 - Special Inquiry/Annuity']" mode="updateDesc">
        <ex:Item><xsl:text>Miscellaneous - Inactive:300 - Verification</xsl:text></ex:Item>
    </xsl:template> 
    <xsl:template match="ListValue[@displayName = 'Commission']" mode="updateDesc">
        <ex:Item><xsl:text>Commission Scheme</xsl:text></ex:Item>
    </xsl:template>
    <xsl:template match="ListValue[@displayName = 'Internal']" mode="updateDesc">
        <ex:Item><xsl:text>Internal Theft</xsl:text></ex:Item>
    </xsl:template>
    <xsl:template match="ListValue[@displayName = 'Misappropriation'] | ListValue[@displayName = 'Theft of Funds']" mode="updateDesc">
        <ex:Item><xsl:text>Misappropriation of Funds</xsl:text></ex:Item>
    </xsl:template>
    <xsl:template match="ListValue[@displayName = 'Investigative Services - General:298 - Locate']" mode="updateDesc">
        <ex:Item><xsl:text>Not Required - Inactive:100 - Closing file</xsl:text></ex:Item>
    </xsl:template>
</xsl:stylesheet>