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



我正试图编写一个只选择一次唯一元素的XSLT(版本1.0)。这种情况是,我正在调用ESB并返回SOAPXML结果。我将结果转换为去掉一堆未使用的元素和属性,最后得到这样的结果:

<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元素,用户可以从中选择他们想要显示的字段。

我相信有一些方法可以用钥匙解决这个问题,但我一辈子都想不出来。提前感谢你的帮助。

这可能相对简单,也可能非常复杂,也可能介于两者之间——这一切都取决于对元素名称的唯一性所做的假设。

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

<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>总是<top>的子级(或<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>

如果不能维持上述假设,那么事情就会变得更加复杂。例如,如果每个级别都可以有自己的<description>元素,那么您将需要向唯一性测试添加更多约束。例如,您可以测试元素名称与其父元素名称连接的唯一性。或者,您可以将元素祖先的计数添加到混合中。

最终,要在没有任何假设的情况下做到这一点,您需要查看元素的整个路径。这需要递归地收集——因此,在开始仅选择唯一元素之前,您需要进行初步处理。

相关内容

  • 没有找到相关文章

最新更新