我有以下XML结构:
<?xml version="1.0" encoding="UTF-8"?>
<globalRequesteeDepartment>
<listItems>
<description type="varchar">HstnOperations</description>
<indexValue type="varchar">JOB COST COORDNTR</indexValue>
</listItems>
<listItems>
<description type="varchar">HstnOperations</description>
<indexValue type="varchar">ENGINEERING COORD</indexValue>
</listItems>
<listItems>
<description type="varchar">HstnOperations</description>
<indexValue type="varchar">OPERATIONS COORDINAT</indexValue>
</listItems>
<listItems>
<description type="varchar">HstnSafety</description>
<indexValue type="varchar">WC COORDINATOR</indexValue>
</listItems>
<listItems>
<description type="varchar">HstnSafety</description>
<indexValue type="varchar">SAFETY DIRECTOR</indexValue>
</listItems>
<listItems>
<description type="varchar">HstnSafety</description>
<indexValue type="varchar">SAFETY SPECIALIST IV</indexValue>
</listItems>
<listItems>
<description type="varchar">HstnEquipment</description>
<indexValue type="varchar">EQUIP MANAGER</indexValue>
</listItems>
<listItems>
<description type="varchar">HstnAccounting</description>
<indexValue type="varchar">CONTROLLER</indexValue>
</listItems>
<listItems>
<description type="varchar">HstnAccounting</description>
<indexValue type="varchar">SR ACCOUNTANT</indexValue>
</listItems>
<listItems>
<description type="varchar">HstnAccounting</description>
<indexValue type="varchar">FINANCIAL ANALYST</indexValue>
</listItems>
</globalRequesteeDepartment>
有可能以及如何使用XSLT获取以下所需的XML输出:
<globalRequesteeDepartment>
<HstnOperations>
<indexValue>JOB COST COORDNTR</indexValue>
<indexValue>ENGINEERING COORD</indexValue>
<indexValue>OPERATIONS COORDINAT</indexValue>
</HstnOperations>
<HstnSafety>
<indexValue>WC COORDINATOR</indexValue>
<indexValue>SAFETY DIRECTOR</indexValue>
<indexValue>SAFETY SPECIALIST IV</indexValue>
</HstnSafety>
<HstnEquipment>
<indexValue>EQUIP MANAGER</indexValue>
</HstnEquipment>
<HstnAccounting>
<indexValue>CONTROLLER</indexValue>
<indexValue>SR ACCOUNTANT</indexValue>
<indexValue>FINANCIAL ANALYST</indexValue>
</HstnAccounting>
</globalRequesteeDepartment>
总结:每个“listItems”都有一个“indexValue”与“description”的联系。我希望能够将每个“描述”值作为自己的元素,但没有重复。然后,每个“listItems”的“indexValue”应该是匹配“description”元素的子元素。
答案 0 :(得分:0)
对于此任务,请使用Muenchian grouping
所以你的XSLT看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- create a key to get all elements grouped -->
<xsl:key name="distinct" match="listItems" use="description" />
<xsl:template match="/globalRequesteeDepartment">
<xsl:copy>
<!-- apply to the first of each group with muenchian grouping-->
<xsl:apply-templates select="listItems[generate-id() = generate-id(key('distinct',description)[1])]" />
</xsl:copy>
</xsl:template>
<xsl:template match="listItems">
<xsl:element name="{description}">
<!-- for-each over one whole group -->
<xsl:for-each select="key('distinct',description)">
<indexValue><xsl:value-of select="indexValue"/></indexValue>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>