2014-06-23 46 views
-1

我正在使用XSLT編寫xml到json解析器。XSLT具體路徑

這裏是我的xml:

<Item> 
<ChildItems> 
    <Item> 
    <Id>child-1</Id> 
    </Item> 
    <Item> 
    <Id>child-2</Id> 
    </Item> 
</ChildItems> 
<Id>main-1</Id> 
</Item> 

我試圖讓主要元素(主要-1)的ID,但我得到的第一ChildItem(子-1)的ID。

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
         xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
         xmlns:custom="custom:v1:interop" version="1.0"> 

    ... 

    "Id": "<xsl:value-of select="//custom:Item/custom:Id" />" 

    ... 

我不能改變的XML,所以請不要認爲這是解決方案。

謝謝。

+2

爲什麼不發佈**完整的**示例,使我們能夠重現您的問題(包括與命名空間相匹配的XSLT的XML)。最重要的是:當你打電話給你的時候(在什麼情況下)。 –

回答

0

假設您發佈了完整的XML,其中Item是根元素,那麼路徑/Item/Id將選擇Item根元素的Id子元素。

0

我遵循你的XSL代碼,並且從Item開始爲Id和相對XPATH編寫絕對XPATH。 我使用你的命名空間自定義,假設你需要它。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:custom="custom:v1:interop" > 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
    <xsl:variable name="myId" select="custom:Item/custom:Id"/> 
    <xsl:template match="custom:Item"> 
    <root> 
    using relative XPATH: <xsl:value-of select="custom:Id"/> 
    using absolute XPATH: <xsl:value-of select="$myId"/> 
    </root> 
    </xsl:template> 
</xsl:stylesheet>