我有多個xml文件需要處理並寫入單個xml文件。我已經完成了大部分轉換,並且在一個xml文件中找到特定元素的地方。使用xslt查找xml文件中的特定元素
一個源XML的是(parts.xml):
<parts>
<part>
<name>head shaft</name>
<code>100</code>
</part>
...
</parts>
另一個源XML(price.xml):
<price-list>
<price>
<part-name>head shaft</part-name>
<cost>28.45</cost>
...
</price>
...
</price-list>
我不得不只獲取代碼元素屬於一個特定的名字元素。 這只是一個源XML文件,像這樣我有很多要處理。
我的輸出XML已經是這樣的(爲result.xml):
<part-order>
<part code=100 name="head shaft" price=32.05 qty=1 />
...
</part-order>
我的XSLT函數來獲取部分代碼:
<xsl:function name="p:find">
<xsl:variable name="partdoc" select="document('parts.xml')"/>
<xsl:param name="str"/>
<xsl:apply-templates select="$partdoc/p:/parts/part[contains(p:name, '$str')]"/>
<xsl:apply-templates select="$partdoc/p:code" />
</xsl:function>
最後,我想調用的函數像這樣:
<xsl:template match="/">
<xsl:copy>
<xsl:variable name="code">
<xsl:value-of select="p:find('head shaft')"/>
</xsl:variable>
<part code="{'$code'}" name="{'head shaft'}" price="{$somelogic}"/>
</xsl:copy>
</xsl:template>
它不工作,因爲我在函數聲明中犯了一些錯誤。你能幫忙嗎?
正在調用key中的文檔引用嗎?當我試圖調用它時,它不顯示任何結果。鑰匙('part-ref','head shaft',$ partdoc)/ code –
@SrikrishnaPothukuchi,我用一個完整的例子編輯了答案,這個例子對我來說工作得很好。如果仍存在問題,請編輯顯示最少但完整的XML示例,您現在擁有的XSLT,輸出的結果以及有關使用過的XSLT處理器的信息。 –
非常感謝馬丁。正如您正確地指出的那樣,這是我的Eclipse的問題。我使用Eclipse Neon使用Saxon 9.7。有時它的行爲不正確。當我使用XML Spear時,相同的代碼以100%的精度工作。 –