我是XSLT新手,但爲工作做了很多XML修改,所以我正在努力學習。我有以下格式輸入XML(有XML上方和下方):刪除與特定節點關聯的所有子元素,並替換爲單個子元素
<Parent Value="x">
<!---->Unlimited number of child elements could exist with any value from 0-1
<Child Value="0.1"/>
<Child Value="1"/>
<Child Value="0.30"/>
...
</Parent>
我需要在以下格式的父/子節點輸出XML:
<Parent Value="x">
<Child Value="0.3"/>
</Parent>
哪裏有隻有一個子元素,它的值是0.3。
我當前XSLT樣的作品:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
<xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Parent">
<Parent>
<xsl:apply-templates select="@*|*"/>
<Child Value="0.3"/>
</Parent>
</xsl:template>
<xsl:template match="Child[@Value!=0.3]"/>
我試圖創建的所有文件所需的子元素(如果它不存在)
<Child Value="0.3"/>
然後只有在其值= 0.3時才輸出子元素。但是,這有時會導致子節點的重複,並且我覺得我可能沒有以正確的方式處理這個問題。另外,xmlns:xs標題出現在父元素之後,而不是出現在輸出文檔的頂部。任何指導將不勝感激!
電流輸出的例子:
<Parent Value="x" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
<Child Value="0.30" />
<Child Value="0.3" />
</Parent>
我不能告訴你這是多麼有用!這完全有道理。非常感謝! – lcf0285 2014-08-27 17:16:14