这只是我第二次使用的XSLT,因此仍然非常绿色。
我有一个看起来像这样的xml:
<?xml version="1.0" encoding="utf-8"?>
<cyber:AOI_INFO xmlns:cyber="http://www.cyberoptics.com/AOI/measure"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:schemaLocation="http://www.cyberoptics.com/AOI/measure c:/xml/Measure.xsd">
<UserName>Machine1\User</UserName>
<SystemId>MACHINE1</SystemId>
<AOIVersion>4.2.1.24</AOIVersion>
<InspectionType>Inspection</InspectionType>
<PositionUnit>Millimeter</PositionUnit>
<RotationUnit>Degree</RotationUnit>
<XYOffsetReference>FeatureOrientation</XYOffsetReference>
<CoplanarityUnit>Angle</CoplanarityUnit>
<Panel Id="4" Name="Panel" PanelStatus="Failed" StartTime="07/09/2018
11:25:53" EndTime="07/09/2018 11:27:40" CycleTime="46" TotalFeatures="10804"
TotalComponents="10804" LotCode="New Lot" LotSize="0" Barcode="Failed to Read
Barcode" Code="Failed to Read Barcode" NG_Stacker_SlotNumber="">
<RecipeName>11047-01_OP10_WFR4_R2</RecipeName>
<RecipeLastUpdateTime></RecipeLastUpdateTime>
<CAD>D:\Panel.XML</CAD>
<PanelResult>
<Attributes>
<FailedBoards Value="1" />
<FailedComponents Value="16" />
<FailedFeatures Value="16" />
</Attributes>
</PanelResult>
<Feature Id="32424" Identifier="BC1" Status="Failed" Value="Failed to Read Barcode" />
<Board Id="1" Name="Board1" BoardStatus="Failed" TotalFeatures="10804" TotalComponents="10804" Barcode="" Skip="false">
<BoardResult>
<Attributes>
<FailedComponents Value="16" />
<FailedFeatures Value="16" />
</Attributes>
</BoardResult>
<Feature Id="2" Identifier="FID0" Status="Passed" />
<Feature Id="3" Identifier="FID1" Status="Passed" />
<Feature Id="4" Identifier="FID2" Status="Passed" />
<Feature Id="5" Identifier="FID3" Status="Passed" />
<Component Id="6" Name="D1" PartNumber="PAD" Package="PAD" Barcode="" IncludedInCurrentVariant="false" ComponentStatus="Passed" TotalFeatures="1">
<ComponentResult>
<Attributes>
<FailedFeatures Value="0" />
</Attributes>
</ComponentResult>
<Feature Id="21606" Identifier="Body1" FeatureStatus="Passed" FeatureInspectionInfo="" MeasurementValid="">
<FeatureResult>
<Inspection Identifier="FeatureLocator" Type="AI2" status="Passed">
<Measurements>
<ConfidenceLevel Value="95.292820242958172" Minimum="1" Maximum="100" Target="35" />
<Polarity IsFailed="false" />
</Measurements>
</Inspection>
<Inspection Identifier="X-POSITION" Type="GeometryMeasurement" status="Passed">
<Measurements>
<ParallelLine Distance="0.24886200808679954" MinDistance="0.23" MaxDistance="0.27" Angle="0.14772531447994211" MaxAngle="90" />
</Measurements>
</Inspection>
<Inspection Identifier="Pad-Gap" Type="GeometryMeasurement" status="Passed">
<Measurements>
<ParallelLine Distance="0.058603726806744492" MinDistance="0.048" MaxDistance="0.08" Angle="0.70909905503968329" MaxAngle="90" />
</Measurements>
</Inspection>
<Inspection Identifier="SR-Opening" Type="GeometryMeasurement" status="Passed">
<Measurements>
<Deviation Value="0.0340010927630135" Threshold="0.5" Type="RMS" />
<Diameter Value="0.55804347481393268" Min="0.4" Max="0.7" />
</Measurements>
</Inspection>
<Inspection Identifier="Appearance" Type="AI2" status="Passed">
<Measurements>
<ConfidenceLevel Value="94.1349731217522" Minimum="1" Maximum="100" Target="50" />
<Polarity IsFailed="false" />
</Measurements>
</Inspection>
</FeatureResult>
</Feature>
</Component>
<BoardPostMeasurementResult Result="Passed" />
</Board>
<PanelPostMeasurementResult Result="Passed" />
</Panel>
</cyber:AOI_INFO>
我几乎拥有想要的XSLT,如下所示:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[parent::*]">
<xsl:element name="{local-name()}">
<xsl:for-each select="attribute::*">
<xsl:element name="{local-name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
<xsl:apply-templates select="node()" />
</xsl:template>
</xsl:stylesheet>
结果是这样的:
<?xml version="1.0" encoding="utf-8"?><cyber:AOI_INFO
xmlns:cyber="http://www.cyberoptics.com/AOI/measure"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:schemaLocation="http://www.cyberoptics.com/AOI/measure
c:/xml/Measure.xsd">
<UserName/>Machine1\User
<SystemId/>MACHINE1
<AOIVersion/>4.2.1.24
<InspectionType/>Inspection
<PositionUnit/>Millimeter
<RotationUnit/>Degree
<XYOffsetReference/>FeatureOrientation
<CoplanarityUnit/>Angle
<Panel><Id>4</Id><Name>Panel</Name><PanelStatus>Failed</PanelStatus>
<StartTime>07/09/2018 11:25:53</StartTime><EndTime>07/09/2018
11:27:40</EndTime><CycleTime>46</CycleTime><TotalFeatures>10804</TotalFeatures>
<TotalComponents>10804</TotalComponents><LotCode>New Lot</LotCode><LotSize>0</LotSize><Barcode>Failed to Read Barcode</Barcode><Code>Failed to Read Barcode</Code><NG_Stacker_SlotNumber/></Panel>
<RecipeName/>11047-01_OP10_WFR4_R2
<RecipeLastUpdateTime/>
<CAD/>D:\Panel.XML
<PanelResult/>
<Attributes/>
<FailedBoards><Value>1</Value></FailedBoards>
<FailedComponents><Value>16</Value></FailedComponents>
<FailedFeatures><Value>16</Value></FailedFeatures>
<Feature><Id>32424</Id><Identifier>BC1</Identifier><Status>Failed</Status><Value>Failed to Read Barcode</Value></Feature>
<Board><Id>1</Id><Name>Board1</Name><BoardStatus>Failed</BoardStatus><TotalFeatures>10804</TotalFeatures><TotalComponents>10804</TotalComponents><Barcode/><Skip>false</Skip></Board>
<BoardResult/>
<Attributes/>
<FailedComponents><Value>16</Value></FailedComponents>
<FailedFeatures><Value>16</Value></FailedFeatures>
<Feature><Id>2</Id><Identifier>FID0</Identifier><Status>Passed</Status></Feature>
<Feature><Id>3</Id><Identifier>FID1</Identifier><Status>Passed</Status></Feature>
<Feature><Id>4</Id><Identifier>FID2</Identifier><Status>Passed</Status></Feature>
<Feature><Id>5</Id><Identifier>FID3</Identifier><Status>Passed</Status></Feature>
<Component><Id>6</Id><Name>D1</Name><PartNumber>PAD</PartNumber><Package>PAD</Package><Barcode/><IncludedInCurrentVariant>false</IncludedInCurrentVariant><ComponentStatus>Passed</ComponentStatus><TotalFeatures>1</TotalFeatures></Component>
<ComponentResult/>
<Attributes/>
<FailedFeatures><Value>0</Value></FailedFeatures>
<Feature><Id>21606</Id><Identifier>Body1</Identifier><FeatureStatus>Passed</FeatureStatus><FeatureInspectionInfo/><MeasurementValid/></Feature>
<FeatureResult/>
<Inspection><Identifier>FeatureLocator</Identifier><Type>AI2</Type><status>Passed</status></Inspection>
<Measurements/>
<ConfidenceLevel><Value>95.292820242958172</Value><Minimum>1</Minimum><Maximum>100</Maximum><Target>35</Target></ConfidenceLevel>
<Polarity><IsFailed>false</IsFailed></Polarity>
<Inspection><Identifier>X-POSITION</Identifier><Type>GeometryMeasurement</Type><status>Passed</status></Inspection>
<Measurements/>
<ParallelLine><Distance>0.24886200808679954</Distance><MinDistance>0.23</MinDistance><MaxDistance>0.27</MaxDistance><Angle>0.14772531447994211</Angle><MaxAngle>90</MaxAngle></ParallelLine>
<Inspection><Identifier>Pad-Gap</Identifier><Type>GeometryMeasurement</Type><status>Passed</status></Inspection>
<Measurements/>
<ParallelLine><Distance>0.058603726806744492</Distance><MinDistance>0.048</MinDistance><MaxDistance>0.08</MaxDistance><Angle>0.70909905503968329</Angle><MaxAngle>90</MaxAngle></ParallelLine>
<Inspection><Identifier>SR-Opening</Identifier><Type>GeometryMeasurement</Type><status>Passed</status></Inspection>
<Measurements/>
<Deviation><Value>0.0340010927630135</Value><Threshold>0.5</Threshold><Type>RMS</Type></Deviation>
<Diameter><Value>0.55804347481393268</Value><Min>0.4</Min><Max>0.7</Max></Diameter>
<Inspection><Identifier>Appearance</Identifier><Type>AI2</Type><status>Passed</status></Inspection>
<Measurements/>
<ConfidenceLevel><Value>94.1349731217522</Value><Minimum>1</Minimum><Maximum>100</Maximum><Target>50</Target></ConfidenceLevel>
<Polarity><IsFailed>false</IsFailed></Polarity>
<BoardPostMeasurementResult><Result>Passed</Result></BoardPostMeasurementResult>
<PanelPostMeasurementResult><Result>Passed</Result></PanelPostMeasurementResult>
</cyber:AOI_INFO>
请注意,某些父元素(例如PanelResult和BoardResult)在其子元素和展平的属性周围没有标签。
我敢肯定,让那些对象像应有的那样围绕其子属性确实非常简单。但是,我玩了足够长的时间,以至于我耗尽了所有想法。
希望有人会认出我在XSLT中的错误,并能够指出正确的方向。
谢谢! 杰瑞
对不起,我认为一些其他细节会有所帮助。这就是我得到的:
<CAD/>D:\Panel.XML
<PanelResult/>
<Attributes/>
<FailedBoards><Value>1</Value></FailedBoards>
<FailedComponents><Value>16</Value></FailedComponents>
<FailedFeatures><Value>16</Value></FailedFeatures>
这就是我想要的:
<CAD>D:\Panel.XML</CAD>
<PanelResult>
<Attributes>
<FailedBoards><Value>1</Value></FailedBoards>
<FailedComponents><Value>16</Value></FailedComponents>
<FailedFeatures><Value>16</Value></FailedFeatures>
</Attributes>
</PanelResult>
答案 0 :(得分:1)
如果我已正确理解,您只想用带有相同名称的元素替换属性,这些元素仅嵌套在其原始父项下的Attributes
元素下。
这应该可以解决问题:请参见XSLT Fiddle Example。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="element()[./@*[namespace-uri() != 'http://www.w3.org/2000/10/XMLSchema-instance' and namespace-uri() != 'xmlns' ]]">
<xsl:copy>
<Attributes>
<xsl:apply-templates select="./@*" />
</Attributes>
<xsl:apply-templates select="./*" />
</xsl:copy>
</xsl:template>
<xsl:template match="@*[namespace-uri() != 'http://www.w3.org/2000/10/XMLSchema-instance' and namespace-uri() != 'xmlns' ]">
<xsl:element name="{local-name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
即(match="@*|node()"
)
匹配所有内容并按原样复制内容,并根据需要将模板应用于该内容。
即(match="element()[./@*[namespace-uri() != 'http://www.w3.org/2000/10/XMLSchema-instance' and namespace-uri() != 'xmlns' ]]"
)
Attributes
标签,而不是为每个属性创建一次。xmlns
过滤掉了所有内容以避免删除名称空间定义,并过滤了xsi
的URI以避免删除标准XML定义;因为它们的目的是作为属性,但您可能不希望它们包含在元素中。即({@*[namespace-uri() != 'http://www.w3.org/2000/10/XMLSchema-instance' and namespace-uri() != 'xmlns']
)
这将匹配属性本身,并将其转换为元素。