在XSLT中只选择一个唯一元素

时间:2014-04-09 18:16:05

标签: xml xslt xpath xslt-1.0

我正在尝试编写一个只选择一次唯一元素的XSLT(版本1.0)。情况是我正在调用ESB并返回SOAP XML结果。我将结果转换为删除一堆未使用的元素和属性,最后得到如下结果:

<feed xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sd="http://ice.exploration.nasa.gov/supplementalData">
<id>uuid:6875e533-7751-446b-9ece-57d414447dd9</id>
<totalResults>8</totalResults>
<startIndex>1</startIndex>
<itemsPerPage>50</itemsPerPage>
<entry>
  <system>Windchill</system>
  <type>NASAModelingandSimulationDocument</type>
  <shortId>297</shortId>
  <eid>uuid:3a26f040-01b1-4b0d-944d-f2c8bd874307</eid>
  <displayName>fedsearchtest NASA Modeling  and Simulation Document-PLM-0000001503(--1)</displayName>
  <url>https://nasa-icedev.nasa.gov/services/feeds/ice/bie/297</url>
  <name>fedsearchtest NASA Modeling  and Simulation Document</name>
  <version>--1</version>
  <description/>
  <creator>Pline, Alex</creator>
  <creationDateTime>2013-12-20T13:39:53-00:00</creationDateTime>
  <comment/>
  <identifier>PLM-0000001503</identifier>
  <modifier>Pline, Alex</modifier>
  <parentContainer>
     <Description>
        <type>Project</type>
        <shortId>123</shortId>
        <eid>uuid:9ca15ace-cfdb-4715-acc1-11f948aea997</eid>
        <displayName>Portal Development</displayName>
        <url>https://nasa-icedev.nasa.gov/services/feeds/ice/bie/123</url>
        <name>Portal Development</name>
        <version/>
        <description>Project</description>
        <creator/>
        <creationDateTime/>
        <comment/>
     </Description>
  </parentContainer>
  <state>INWORK</state>
  <status>c/i</status>
  <updateDateTime>2013-12-20T13:39:53-00:00</updateDateTime>
  <Document>
     <detailsURL>https://icedev.ndc.nasa.gov/Windchill/app/#ptc1/tcomp/infoPage?oid=VR:wt.doc.WTDocument:418523</detailsURL>
     <sbuCui>NULL</sbuCui>
     <exportControlEar>NULL</exportControlEar>
     <exportControlItar>NULL</exportControlItar>
     <NASADocument>
        <author/>
        <bookManager/>
        <cageCode/>
        <category/>
        <cmControlLevel>Not Applicable</cmControlLevel>
        <comments/>
        <controlAuthority/>
        <coverDate>2011-03-18 00:00:00 CDT</coverDate>
        <dataType>Not a Deliverable</dataType>
        <drdNumber/>
        <legacyDes/>
        <opr>Not Applicable</opr>
        <oprOrgCode/>
        <owningCenter/>
        <owningProject/>
        <record>false</record>
        <recordAfsNumber/>
        <recordCutOffDate/>
        <recordLocation/>
        <recordRetentionInstructions/>
        <recordSchedule/>
        <recordStatus/>
        <recordType/>
        <releaseDate/>
        <remotelyReleased>No</remotelyReleased>
        <subcategory/>
        <wbsNumber/>
        <NASAModelingandSimulationDocument>
           <credibilityLevel/>
           <criticalityLevel/>
           <developmentStatus>Not Applicable</developmentStatus>
           <externalUrl/>
           <fileFormat>Other</fileFormat>
           <fileFormatOther/>
           <issueTrackingUrl/>
           <modelSimToolAbbr/>
           <modelSimToolName/>
           <modelSimToolType/>
           <modelSimToolTypeOther/>
           <modelVersion/>
           <operatingSystems/>
           <otherAnalysisEfforts/>
           <pedigree/>
           <pocEmail/>
           <pocName/>
           <pocOrganization/>
           <pocPhone/>
           <programmingLanguage/>
           <proprietaryInformation/>
           <softwareClassification/>
           <tdsNumber/>
           <vvLevel/>
        </NASAModelingandSimulationDocument>
     </NASADocument>
     <files>
        <Bag>
           <Description>
              <type>File</type>
              <shortId/>
              <eid/>
              <displayName>Excel Document copy.xlsx</displayName>
              <url/>
              <name>Excel Document copy.xlsx</name>
              <version>2</version>
              <description/>
              <creator>Pline, Alex</creator>
              <creationDateTime>2013-12-20 13:39:53 CST</creationDateTime>
              <modifier>Pline, Alex</modifier>
              <parentContainer/>
              <updateDateTime>2013-12-20 14:02:40 CST</updateDateTime>
              <checksum>0</checksum>
              <downloadUrl>https://icedev.ndc.nasa.gov/Windchill/servlet/WindchillAuthGW/wt.content.ContentHttp/viewContent/Excel%20Document%20copy.xlsx?u8&amp;HttpOperationItem=wt.content.ApplicationData%3A418541&amp;ContentHolder=wt.doc.WTDocument%3A418524&amp;originalFileName=Excel+Document+copy.xlsx&amp;forceDownload=true</downloadUrl>
              <fileSize>26,416</fileSize>
              <format>xlsx</format>
              <role>PRIMARY</role>
           </Description>
        </Bag>
     </files>
     <parentFolder>
        <Description>
           <type>Folder</type>
           <shortId>125</shortId>
           <eid>uuid:76904fcc-338f-4f3e-a1c6-46c7de7d0a5a</eid>
           <displayName>Federated Search Test</displayName>
           <url>https://nasa-icedev.nasa.gov/services/feeds/ice/bie/125</url>
           <name>Federated Search Test</name>
           <version/>
           <description>Folder</description>
           <creator/>
           <creationDateTime/>
           <comment/>
        </Description>
     </parentFolder>
     <references/>
     <referencedBy/>
     <uses/>
     <usedBy/>
  </Document>
</entry>
<entry>
  <system>Liferay</system>
  <type>Part</type>
  <shortId>3105</shortId>
  <eid>uuid:9b080a96-5425-43fd-a3cb-3bca2cd073b6</eid>
  <displayName>fedsearchtest Part-0000000121(A-1)</displayName>
  <url>https://nasa-icedev.nasa.gov/services/feeds/ice/bie/3105</url>
  <name>fedsearchtest Part</name>
  <version>A-1</version>
  <description/>
  <creator>Pline, Alex</creator>
  <creationDateTime>2013-12-13T13:14:57-00:00</creationDateTime>
  <comment/>
  <identifier>0000000121</identifier>
  <modifier>Pline, Alex</modifier>
  <parentContainer>
     <Description>
        <type>Project</type>
        <shortId>123</shortId>
        <eid>uuid:9ca15ace-cfdb-4715-acc1-11f948aea997</eid>
        <displayName>Portal Development</displayName>
        <url>https://nasa-icedev.nasa.gov/services/feeds/ice/bie/123</url>
        <name>Portal Development</name>
        <version/>
        <description>Project</description>
        <creator/>
        <creationDateTime/>
        <comment/>
     </Description>
  </parentContainer>
  <state>INWORK</state>
  <status>c/i</status>
  <updateDateTime>2013-12-13T13:14:57-00:00</updateDateTime>
  <Part>
     <detailsURL>https://icedev.ndc.nasa.gov/Windchill/app/#ptc1/tcomp/infoPage?oid=VR:wt.part.WTPart:417193</detailsURL>
     <sbuCui>NULL</sbuCui>
     <exportControlEar>NULL</exportControlEar>
     <exportControlItar>NULL</exportControlItar>
     <alternateIdentifier/>
     <assemblyMode>separable</assemblyMode>
     <bomType/>
     <classification/>
     <classificationOrganization/>
     <classificationReason/>
     <classifiedBy/>
     <contractNumber/>
     <contributor/>
     <coverage/>
     <date/>
     <dateApplied/>
     <defaultTraceCode>0</defaultTraceCode>
     <defaultUnit>ea</defaultUnit>
     <endItem>false</endItem>
     <findNumber/>
     <jobAuthorizationNumber/>
     <maximumAllowed/>
     <minimumRequired/>
     <ownedBy/>
     <owner/>
     <phantomPart>false</phantomPart>
     <phase/>
     <publisher/>
     <quantity/>
     <referenceDesignator/>
     <relation/>
     <representation/>
     <rights/>
     <source>make</source>
     <subject/>
     <supplierId/>
     <unit/>
     <files/>
     <parentFolder>
        <Description>
           <type/>
           <shortId>3029</shortId>
           <eid>uuid:eb7590ed-53fd-4669-9be9-e190b02d7bb6</eid>
           <displayName>Federated Search Test</displayName>
           <url>https://nasa-icedev.nasa.gov/services/feeds/ice/bie/3029</url>
           <name>Federated Search Test</name>
           <version/>
           <description>Folder</description>
           <creator/>
           <creationDateTime/>
           <comment/>
        </Description>
     </parentFolder>
     <describes/>
     <describedBy/>
     <referencedBy/>
     <references/>
     <usedBy/>
     <uses/>
  </Part>
</entry>
<entry>
  <system>Liferay</system>
  <type>Part</type>
  <shortId>3107</shortId>
  <eid>uuid:a828443a-5b8f-4ff7-a72f-39b9aef0f12f</eid>
  <displayName>fedsearchtest NASA Part-0000000122(1-1)</displayName>
  <url>https://nasa-icedev.nasa.gov/services/feeds/ice/bie/3107</url>
  <name>fedsearchtest NASA Part</name>
  <version>1-1</version>
  <description/>
  <creator>Pline, Alex</creator>
  <creationDateTime>2013-12-13T13:15:19-00:00</creationDateTime>
  <comment/>
  <identifier>0000000122</identifier>
  <modifier>Pline, Alex</modifier>
  <parentContainer>
     <Description>
        <type>Project</type>
        <shortId>123</shortId>
        <eid>uuid:9ca15ace-cfdb-4715-acc1-11f948aea997</eid>
        <displayName>Portal Development</displayName>
        <url>https://nasa-icedev.nasa.gov/services/feeds/ice/bie/123</url>
        <name>Portal Development</name>
        <version/>
        <description>Project</description>
        <creator/>
        <creationDateTime/>
        <comment/>
     </Description>
  </parentContainer>
  <state>INWORK</state>
  <status>c/i</status>
  <updateDateTime>2013-12-13T13:15:19-00:00</updateDateTime>
  <Part>
     <detailsURL>https://icedev.ndc.nasa.gov/Windchill/app/#ptc1/tcomp/infoPage?oid=VR:wt.part.WTPart:417404</detailsURL>
     <sbuCui>NULL</sbuCui>
     <exportControlEar>NULL</exportControlEar>
     <exportControlItar>NULL</exportControlItar>
     <alternateIdentifier/>
     <assemblyMode>separable</assemblyMode>
     <bomType/>
     <classification/>
     <classificationOrganization/>
     <classificationReason/>
     <classifiedBy/>
     <contractNumber/>
     <contributor/>
     <coverage/>
     <date/>
     <dateApplied/>
     <defaultTraceCode>0</defaultTraceCode>
     <defaultUnit>ea</defaultUnit>
     <endItem>false</endItem>
     <findNumber/>
     <jobAuthorizationNumber/>
     <maximumAllowed/>
     <minimumRequired/>
     <ownedBy/>
     <owner/>
     <phantomPart>false</phantomPart>
     <phase/>
     <publisher/>
     <quantity/>
     <referenceDesignator/>
     <relation/>
     <representation/>
     <rights/>
     <source>make</source>
     <subject/>
     <supplierId/>
     <unit/>
     <files/>
     <parentFolder>
        <Description>
           <type/>
           <shortId>3029</shortId>
           <eid>uuid:eb7590ed-53fd-4669-9be9-e190b02d7bb6</eid>
           <displayName>Federated Search Test</displayName>
           <url>https://nasa-icedev.nasa.gov/services/feeds/ice/bie/3029</url>
           <name>Federated Search Test</name>
           <version/>
           <description>Folder</description>
           <creator/>
           <creationDateTime/>
           <comment/>
        </Description>
     </parentFolder>
     <describes/>
     <describedBy/>
     <referencedBy/>
     <references/>
     <usedBy/>
     <uses/>
  </Part>
</entry>
<entry>
  <system>Liferay</system>
  <type>Part</type>
  <shortId>3109</shortId>
  <eid>uuid:9e47aa11-4328-4712-89dd-6277c789746d</eid>
  <displayName>fedsearchtest End Item-0000000123(A-1)</displayName>
  <url>https://nasa-icedev.nasa.gov/services/feeds/ice/bie/3109</url>
  <name>fedsearchtest End Item</name>
  <version>A-1</version>
  <description/>
  <creator>Pline, Alex</creator>
  <creationDateTime>2013-12-13T13:15:52-00:00</creationDateTime>
  <comment/>
  <identifier>0000000123</identifier>
  <modifier>Pline, Alex</modifier>
  <parentContainer>
     <Description>
        <type>Project</type>
        <shortId>123</shortId>
        <eid>uuid:9ca15ace-cfdb-4715-acc1-11f948aea997</eid>
        <displayName>Portal Development</displayName>
        <url>https://nasa-icedev.nasa.gov/services/feeds/ice/bie/123</url>
        <name>Portal Development</name>
        <version/>
        <description>Project</description>
        <creator/>
        <creationDateTime/>
        <comment/>
     </Description>
  </parentContainer>
  <state>INWORK</state>
  <status>c/i</status>
  <updateDateTime>2013-12-13T13:15:52-00:00</updateDateTime>
  <Part>
     <detailsURL>https://icedev.ndc.nasa.gov/Windchill/app/#ptc1/tcomp/infoPage?oid=VR:wt.part.WTPart:417427</detailsURL>
     <sbuCui>NULL</sbuCui>
     <exportControlEar>NULL</exportControlEar>
     <exportControlItar>NULL</exportControlItar>
     <alternateIdentifier/>
     <assemblyMode>separable</assemblyMode>
     <bomType/>
     <classification/>
     <classificationOrganization/>
     <classificationReason/>
     <classifiedBy/>
     <contractNumber/>
     <contributor/>
     <coverage/>
     <date/>
     <dateApplied/>
     <defaultTraceCode>S</defaultTraceCode>
     <defaultUnit>ea</defaultUnit>
     <endItem>true</endItem>
     <findNumber/>
     <jobAuthorizationNumber/>
     <maximumAllowed/>
     <minimumRequired/>
     <ownedBy/>
     <owner/>
     <phantomPart>false</phantomPart>
     <phase/>
     <publisher/>
     <quantity/>
     <referenceDesignator/>
     <relation/>
     <representation/>
     <rights/>
     <source>make</source>
     <subject/>
     <supplierId/>
     <unit/>
     <files/>
     <parentFolder>
        <Description>
           <type/>
           <shortId>3029</shortId>
           <eid>uuid:eb7590ed-53fd-4669-9be9-e190b02d7bb6</eid>
           <displayName>Federated Search Test</displayName>
           <url>https://nasa-icedev.nasa.gov/services/feeds/ice/bie/3029</url>
           <name>Federated Search Test</name>
           <version/>
           <description>Folder</description>
           <creator/>
           <creationDateTime/>
           <comment/>
        </Description>
     </parentFolder>
     <describes/>
     <describedBy/>
     <referencedBy/>
     <references/>
     <usedBy/>
     <uses/>
  </Part>
</entry>
</feed>

请注意,其他条目元素可以包含其他子元素,具体取决于其类型。

我希望XSLT做的是创建一个单独的条目,其中包含源文档中的条目元素中存在的所有可能元素。因此,在上述XML的情况下,输出条目看起来像这样:

<entry>
  <system>Windchill</system>
  <type>Document</type>
  <shortId>298</shortId>
  <parentContainer>
    ..
  </parentContainer>
  <Document>
    <NASADocument>
      ..
      <NASAModelingandSimulationDocument>
        ..
      </NASAModelingandSimulationDocument>
    </NASADocument>
    <files>
      ..
    </files>
    <parentFolder>
      ..
    </parentFolder>
  </Document>
  <Part>
    ..
  </Part>
</entry>

基本上,我正在尝试创建一个具有条目记录可能具有的所有潜在字段的条目记录。这些字段的值并不重要,因为我正在构建此结果以填充UI元素,用户可以从中选择要显示的字段。

我确信有一些方法可以使用键来解决这个问题,但我无法弄清楚我的生活。在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这可能相对简单或非常复杂或介于两者之间 - 所有这些都取决于可以对元素名称的唯一性做出什么假设。

让我从我认为最简单的情况开始,我们可以假设每个元素名称对于文档层次结构中的级别是唯一的。在以下示例中:

<root>
    <top id="1">
        <middle/>
    </top>
    <top id="2">
        <middle>
            <low/>
        </middle>
    </top>
    <top id="3">
        <middle>
            <bottom/>
        </middle>
    </top>
    <top id="4">
        <middle>
            <bottom>
                <rock/>
            </bottom>
        </middle>
    </top>
</root>

我们假设<top>只能 作为<root>的孩子,<middle> 始终是{的孩子{ {1}}(或<top>的兄弟姐妹),依此类推。在这种假设下,我们可以&#34;映射&#34;上述结构相对容易:

<top>

生产:

<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="sameName" match="*" use="local-name()" />

<xsl:template match="*">
    <xsl:copy>
        <xsl:apply-templates select="key('sameName', local-name())/*[count(. | key('sameName', local-name())[1]) = 1]"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

如果无法维持上述假设,那么事情会变得更加复杂。例如,如果每个级别都有自己的<?xml version="1.0" encoding="UTF-8"?> <root> <top> <middle> <low/> <bottom> <rock/> </bottom> </middle> </top> </root> 元素,那么您需要为唯一性测试添加更多约束。例如,您可以测试元素名称与其父级名称连接的唯一性。或者你可以添加元素的祖先的数量。

最终,要完成此操作而不需要任何假设,您需要查看元素的整个路径。这需要递归收集 - 因此您需要先进行初步处理,然后才能开始选择唯一元素。