2012-07-31 108 views
1

我有輸入XML文件。如下。查詢xpath,xml

<maindocument> 
<first> 
    <testing>random text</testing> 
    <checking>random test</checking> 
</first> 
<testing> 
<testing>sample</testing> 
<checking>welcome</checking> 
</testing> 
<import> 
    <downloading>valuable text</downloading> 
</import> 
</maindocument> 

這裏是輸出XML我想

<maindocument> 
<import> 
    <doctype>Valuable</doctype> 
    <docint>text</docint> 
</import> 
</maindocument> 

當我在谷歌搜索,我得到的結果爲XSL:Copy

+0

轉換的可定位邏輯是什麼? – Utkanos 2012-07-31 12:58:58

+0

您的整體需求不是很清楚。看起來你想丟棄''中的所有內容,除了''的內容,並轉換一些方法。但什麼是轉化的'內容'到標準''和'' psubsee2003 2012-07-31 13:01:37

+0

使用XSLT的使用絕對或相對路徑我只需要從元素 – karthic 2012-07-31 13:01:50

回答

1

這種轉變

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="import"> 
    <maindocument> 
     <xsl:copy> 
     <doctype><xsl:value-of select="substring-before(*, ' ')"/></doctype> 
     <docint><xsl:value-of select="substring-after(*, ' ')"/></docint> 
     </xsl:copy> 
    </maindocument> 
</xsl:template> 
<xsl:template match="text()"/> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<maindocument> 
    <first> 
     <testing>random text</testing> 
     <checking>random test</checking> 
    </first> 
    <testing> 
     <testing>sample</testing> 
     <checking>welcome</checking> 
    </testing> 
    <import> 
     <downloading>valuable text</downloading> 
    </import> 
</maindocument> 

produc es想要的,正確的結果:

<maindocument> 
    <import> 
     <doctype>valuable</doctype> 
     <docint>text</docint> 
    </import> 
</maindocument> 
+0

取數據的幫助謝謝大家 – karthic 2012-08-01 04:26:23

+0

@karthic:不用謝。 – 2012-08-01 04:38:13

2

嘗試...

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes" /> 
<xsl:strip-space elements="*" /> 

<xsl:template match="@*|node()"> 
<xsl:copy> 
    <xsl:apply-templates select="@*|node()" /> 
</xsl:copy> 
</xsl:template> 

<xsl:template match="first|testing|checking" /> 

<xsl:template match="import"> 
<xsl:copy> 
    <doctype><xsl:value-of select="substring-before(.,' ')" /></doctype> 
    <docint><xsl:value-of select="substring-after(.,' ')" /></docint> 
</xsl:copy> 
</xsl:template> 

</xsl:stylesheet>