我想对具有多个重复元素的XML执行XSLT转换。我需要匹配@id
和Records/Metadata
下的Record/Field
属性才能获得相应的输出。我需要显示所有字段名称及其输出。
我是XSLT的新手,XSLT中的任何辅助都会非常有用。下面是输入XML和所需的输出。
输入:
<Records count="40">
<Metadata>
<FieldDefinitions>
<FieldDefinition alias="Finding_ID" name="Finding ID" guid="a69370c5-b6a7-4e20-a073-dd6bbd131e43" id="2260" />
<FieldDefinition alias="Status" name="Status" guid="68ecb49e-4eb6-492b-986f-43bf4a2f0d1f" id="2269" />
<FieldDefinition alias="Last_Updated" name="Last Updated" guid="b90f8e15-58d6-4fd6-b1d8-e9b008629723" id="2276" />
<FieldDefinition alias="Date_Closed" name="Date Closed" guid="f54c94c8-314f-4328-831b-ded5179fa95b" id="11224" />
<FieldDefinition alias="Expected_Completion_Date" name="Expected Completion Date" guid="a5ecc11f-ab40-483d-aff5-2f0a8e56e8d9" id="14653" />
<FieldDefinition alias="Actual_Completion_Date" name="Actual Completion Date" guid="6fc586cf-5709-47a9-a84c-a6b83141ce1d" id="14654" />
<FieldDefinition alias="Action_Plan_Review_Status" name="Action Plan Review Status" guid="82cc6d74-5881-4b23-9efb-e2b081d83e3f" id="30799" />
<FieldDefinition alias="IC_Description" name="Corrective Action Plan" guid="bfb7b09b-b728-4dbf-a016-3ee232948793" id="14655" />
<FieldDefinition alias="Action_Plan_Status" name="Action Plan Status" guid="49c3b62c-4828-41e0-bcbc-3dbcf37fc08d" id="27953" />
<FieldDefinition alias="Audit_Date" name="Review Date" guid="0d7ad11f-0d8c-4cc8-a51b-8ceeddafa9b1" id="27955" />
<FieldDefinition alias="Questionnaire_ID" name="Questionnaire ID" guid="a6e1f48e-08b7-45b8-b885-b9368f4cea2c" id="30178" />
<FieldDefinition alias="Question_Name" name="Question Name" guid="9136a544-cc8e-442d-89af-4bb9c64b4a2e" id="30179" />
<FieldDefinition alias="Action_Plan_Reviewer_1" name="Action Plan Reviewer" guid="d3ae8a48-c0c8-45c5-96f8-29d1d6f1a086" id="30789" />
<FieldDefinition alias="Facility_Name" name="Facility Name" guid="8cfba154-fc68-467c-810e-a9c6c69aa8a2" id="129" />
</FieldDefinitions>
</Metadata>
<LevelCounts>
<LevelCount count="40" guid="b085b230-e20f-41df-a849-f5d6811447ea" id="62" />
<LevelCount count="40" guid="7eb2544d-95aa-4122-9ee5-4f458f9cb1bc" id="60" />
</LevelCounts>
<Record parentId="0" moduleId="167" levelGuid="b085b230-e20f-41df-a849-f5d6811447ea" levelId="62" contentId="288373">
<Record parentId="0" moduleId="69" levelGuid="7eb2544d-95aa-4122-9ee5-4f458f9cb1bc" levelId="60" contentId="240937">
<Field guid="8cfba154-fc68-467c-810e-a9c6c69aa8a2" id="129" type="1">5000-AR</Field>
</Record>
<Field guid="a69370c5-b6a7-4e20-a073-dd6bbd131e43" id="2260" type="6">4035</Field>
<Field guid="a6e1f48e-08b7-45b8-b885-b9368f4cea2c" id="30178" type="1">288237</Field>
<Field guid="9136a544-cc8e-442d-89af-4bb9c64b4a2e" id="30179" type="1">chemistry</Field>
<Field guid="82cc6d74-5881-4b23-9efb-e2b081d83e3f" id="30799" type="4" />
<Field guid="68ecb49e-4eb6-492b-986f-43bf4a2f0d1f" id="2269" type="4">
<ListValues>
<ListValue id="1660" displayName="Open">Open</ListValue>
</ListValues>
</Field>
<Field guid="49c3b62c-4828-41e0-bcbc-3dbcf37fc08d" id="27953" type="4">
<ListValues>
<ListValue id="101940" displayName="Open">Open</ListValue>
</ListValues>
</Field>
<Field guid="bfb7b09b-b728-4dbf-a016-3ee232948793" id="14655" type="1" />
<Field guid="a5ecc11f-ab40-483d-aff5-2f0a8e56e8d9" id="14653" type="3" />
<Field guid="6fc586cf-5709-47a9-a84c-a6b83141ce1d" id="14654" type="3" />
<Field guid="f54c94c8-314f-4328-831b-ded5179fa95b" id="11224" type="3" />
<Field guid="b90f8e15-58d6-4fd6-b1d8-e9b008629723" id="2276" type="22" xmlConvertedValue="2014-11-02T08:50:03.707Z">11/2/2014 2:50:03 AM</Field>
<Field guid="d3ae8a48-c0c8-45c5-96f8-29d1d6f1a086" id="30789" type="8">
<Users>
<User id="217" domain="xyz" lastName="abc" firstName="h">habc</User>
</Users>
</Field>
<Field guid="0d7ad11f-0d8c-4cc8-a51b-8ceeddafa9b1" id="27955" type="3" xmlConvertedValue="2014-05-12T00:00:00Z">5/12/2014 12:00:00 AM</Field>
</Record>
</Records>
输出:
<Record>
<Data>
<Finding_id>4035</Finding_ID>
<Questionnaire_ID>288237<Questionnaire_ID>
<Question_Name>chemistry<Question_Name>
<!-- and so on -->
</Data>
</Record>
答案 0 :(得分:0)
很难理解输入的结构。只有一个记录(尽管计数表示为40),并且它内嵌了另一个记录。我不确定你究竟想要怎样处理这件事。不过,您可以使用以下内容作为起点:
XSLT 1.0
的编辑 强>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="fname" match="FieldDefinition" use="@guid" />
<xsl:template match="/">
<root>
<xsl:for-each select="Records/Record">
<Record>
<xsl:apply-templates select="Field | Record/Field"/>
</Record>
</xsl:for-each>
</root>
</xsl:template>
<xsl:template match="Field">
<xsl:element name="{key('fname', @guid)/@alias}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="Field[key('fname', @guid)/@alias='Action_Plan_Reviewer_1']">
<Action_Plan_Reviewer_1>
<xsl:value-of select="concat(Users/User/@lastName, ' ', Users/User/@firstName)"/>
</Action_Plan_Reviewer_1>
</xsl:template>
</xsl:stylesheet>
应用于您的示例输入,结果为:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Record>
<Finding_ID>4035</Finding_ID>
<Questionnaire_ID>288237</Questionnaire_ID>
<Question_Name>chemistry</Question_Name>
<Action_Plan_Review_Status/>
<Status>Open</Status>
<Action_Plan_Status>Open</Action_Plan_Status>
<IC_Description/>
<Expected_Completion_Date/>
<Actual_Completion_Date/>
<Date_Closed/>
<Last_Updated>11/2/2014 2:50:03 AM</Last_Updated>
<Action_Plan_Reviewer_1>abc h</Action_Plan_Reviewer_1>
<Audit_Date>5/12/2014 12:00:00 AM</Audit_Date>
</Record>
</root>
注意强>:
如果guid值是常量,则可以简化模式:
<xsl:template match="Field[key('fname', @guid)/@alias='Action_Plan_Reviewer_1']">
为:
<xsl:template match="Field[@guid='d3ae8a48-c0c8-45c5-96f8-29d1d6f1a086']">