我有一個關於XSLT的問題。我是一個新手。閱讀論壇並試圖獲得所需的XML格式輸出。XSLT將複雜的實時XML轉換爲有意義的平面XML
如您所見,根據$<DataSources>
標籤屬性TYPEID
,其子NODES
有所不同。
我想在這裏實現的是
- FLATTEN XML的層次
- 了把某些標記
轉換標籤,如
<expression>
又包含其他子節點或標籤,而不是一個TEXT()值,以這種格式(AND)的表達。((((EventDisplayNumber)(等於)(823)))... ..和等
到目前爲止,我只能將所有屬性轉換爲ELEMENTS,作爲平展XML的第一步。
下一步是打破heirarchy並將深嵌套的XML標籤轉換爲數學符號的符號表達式。
第三種方法是從以前的輸出中選擇所需的NODES。
請建議或幫助,因爲我完全難以接受,因此我應該如何繼續下一步。
輸入::
<Rule ID="SpaceLeftPercent." Enabled="true" Target="DBFileGroup" >
<Category>PerformanceCollection</Category>
<DataSources>
<DataSource ID="DS" TypeID="DBFileGroupSizeOptimizedPerfProvider">
<IntervalSeconds>900</IntervalSeconds>
<SyncTime />
<ConnectionString> ConnectionString$</ConnectionString>
<ServerName> NetworkName$</ServerName>
<SqlInstanceName> InstanceName$</SqlInstanceName>
<ObjectName> PerformanceCounterObject$ : Database : File Group</ObjectName>
<CounterName>DB File Group Allocated Space Left (%)</CounterName>
<InstanceName> GroupName$</InstanceName>
<DatabaseName> DatabaseName$</DatabaseName>
<DBFileGroupId>GroupID$</DBFileGroupId>
<Value>$Data/Property[@Name='FreeSpaceAutoGrowPercent']$</Value>
<Tolerance>10</Tolerance>
<ToleranceType>Absolute</ToleranceType>
<MaximumSampleSeparation>4</MaximumSampleSeparation>
<TimeoutSeconds>300</TimeoutSeconds>
</DataSource>
</DataSources>
</Rule>
<Rule ID=" id823" Enabled="true" ConfirmDelivery="true" Remotable="true" >
<Category>EventCollection</Category>
<DataSources>
<DataSource ID="DS" TypeID="Windows!Microsoft.Windows.EventProvider">
<ComputerName>NetworkName$</ComputerName>
<LogName>Application</LogName>
<Expression>
<And>
<Expression>
<SimpleExpression>
<ValueExpression>
<XPathQuery Type="UnsignedInteger">EventDisplayNumber</XPathQuery>
</ValueExpression>
<Operator>Equal</Operator>
<ValueExpression>
<Value Type="UnsignedInteger">823</Value>
</ValueExpression>
</SimpleExpression>
</Expression>
<Expression>
<SimpleExpression>
<ValueExpression>
<XPathQuery Type="String">PublisherName</XPathQuery>
</ValueExpression>
<Operator>Equal</Operator>
<ValueExpression>
<Value Type="String"> ServiceName$</Value>
</ValueExpression>
</SimpleExpression>
</Expression>
</And>
</Expression>
</DataSource>
</DataSources>
<WriteActions>
<WriteAction >
<Priority>2</Priority>
<Severity>2</Severity>
<AlertName />
<AlertDescription />
<AlertOwner />
<AlertParameters>
<AlertParameter1>$Data/EventDescription$</AlertParameter1>
</AlertParameters>
<Suppression>
<SuppressionValue />
</Suppression>
</WriteAction>
</WriteActions>
</Rule>
我目前的XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*/Rule/DataSources/DataSource">
<xsl:element name="{local-name()}">
<xsl:value-of select="."/>
</xsl:element>
<xsl:apply-templates select="node()"/>
</xsl:template>
<xsl:template name="resolveexpr" match="/*/Rule/DataSources/DataSource">
<xsl:choose>
<xsl:when test="contains(name(.),'Expression')">
<xsl:text>sometext</xsl:text>
</xsl:when>
<xsl:otherwise />
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
輸出DESIRED: 類似下面。 但是我的最終目標是把它帶入Excel表格。
<Rule>
<ID>id823</ID>
<Enabled>true</Enabled>
<ConfirmDelivery>true</ConfirmDelivery>
<Remotable>true</Remotable >
<Category>EventCollection</Category>
<DataSources>
<DataSource>
<TypeID>Windows!Microsoft.Windows.EventProvider>
<ComputerName>NetworkName$</ComputerName>
<LogName>Application</LogName>
<Expression>
<And>((((EventDisplayNumber))(Equal)((823))))((((PublisherName))(Equal)((ServiceName$))))</And>
</Expression>
</DataSource>
</DataSources>
<WriteActions>
<WriteAction >
<Priority>2</Priority>
<Severity>2</Severity>
<AlertParameters>
<AlertParameter1>$Data/EventDescription$</AlertParameter1>
</AlertParameters>
<Suppression>
<SuppressionValue />
</Suppression>
</WriteAction>
</WriteActions>
</Rule>
你能在這種情況下顯示你期望的輸出嗎?如果您也縮減了XML樣本,它也可能有所幫助。謝謝! –
單位通常沒有意義。層次結構傳達上下文。 – keshlam
@Tim我添加了所需的輸出,也是縮進的。 – user3391883