我很喜歡用xpath或xslt進行編程。我試圖選擇只有指定的節點進行處理,並在該過程中創建每個元素的文件夾。下面是我的測試XML IM與合作:只選擇一個節點進行處理
XML:
<Sandbox>
<Unknow name="Unknow">
<Property name="unknow" value="unknow"/>
</Unknow>
<View name="Object">
<Element name="first" value="1">
<Property name="great" value="10"/>
<Element name="detail" value="3">
<Property name="shiny" value="30"/>
<Element name="doNot" value="0">
<Property name="non" value="0"/>
</Element>
</Element>
</Element>
</View>
<View name="OtherObject">
<Element name="second" value="2">
<Property name="greater" value="20"/>
<Element name="detail" value="4">
<Property name="dark" value="40"/>
<Element name="doNot" value="0">
<Property name="non" value="0"/>
</Element>
</Element>
</Element>
</View>
</Sandbox>
XSLT
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="Sandbox/View[@name='OtherObject']">
<xsl:apply-templates select="//Element"/>
</xsl:template>
<xsl:template match="Element">
<xsl:result-document href="{string-join(ancestor-or-self::Element,'/')}/{concat(@name,'_',position())}.xml">
<item>
<xsl:copy-of select="."/>
</item>
</xsl:result-document>
</xsl:template>
我想有輸出是一個文件夾結構/對象/首/細節。我究竟做錯了什麼?我收到一個錯誤,我無法創建輸出文件。你有什麼建議xpath cuz即時通訊猜測我的xpath不正確。由於
您的路徑不包含「doNot」(其中一個元素@name)。你想排除它嗎?此外,您的第一個模板的「OtherObject」爲@name ..但您想要的文件夾路徑具有「Object」。你真的想要處理哪個節點? – 2014-10-28 09:33:31