2012-01-22 75 views
4

我試圖根據決策xml文件(Input2)中可用的數據從主XML文件(Input1)生成輸出XML文件。以2個XML文件作爲輸入並生成輸出XML文件的XSLT

萬事達文件

<Level1> 

<Level2> 
    <LinkedTo>DATA1</LinkedTo> <!DATA1 in the decision file> 
    <Attribute1>1</Attribute1> 
    <Attribute2>2</Attribute2> 
</Level2> 

<Level2> 
    <LinkedTo>DATA2</LinkedTo> 
    <Attribute1>3</Attribute1> 
    <Attribute2>4</Attribute2> 
</Level2> 

</Level1> 

決策文件:

<TopLevel> 
<DATA1> 
    <Available>Y</Available> 
</DATA1> 

<DATA2> 
    <Available>N</Available> 
</DATA2> 

</TopLevel> 

的XSLT處理時必須輸出結果文件(基於一個YES或在決策文件否)。

<Level1> 
<Level2> 
    <Attribute1>1</Attribute1> 
    <Attribute2>2</Attribute2> 
</Level2> 
</Level1> 

我必須承認我從來沒有做過XML的東西,但這是可行性研究所需要的。 XSLT應該是什麼?我可以使用你的答案並擴展這個概念。或者如果有其他選擇(python,C#,C,C++等),那麼也歡迎這些。我可以用C/C++或任何面向過程的語言進行管理。

回答

6

使用document功能。通過URI來決定XML,例如:

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

    <xsl:template match="Level1"> 
    <xsl:copy> 
     <xsl:apply-templates select="Level2"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Level2"> 
    <xsl:if test="document('Decision.xml')/TopLevel/*[ 
     name() = current()/LinkedTo and Available = 'Y']"> 
     <xsl:copy> 
     <xsl:apply-templates select="*[not(self::LinkedTo)]"/> 
     </xsl:copy> 
    </xsl:if> 
    </xsl:template> 

    <xsl:template match="*"> 
    <xsl:copy-of select="."/> 
    </xsl:template> 

</xsl:stylesheet> 
+1

我的道歉,我已經離開了一段時間了。這兩個解決方案(基里爾和馬丁)都爲我做了一點小小的調整。我也在研究其他方法,比如我的問題陳述的UML建模。 – Raj

2

作爲替換,在這裏是可與XSLT使用2.0處理器等撒克遜9,AltovaXML,XQSharp一個XSLT 2.0溶液:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="2.0"> 

<xsl:param name="dec-file" select="'decision.xml'"/> 
<xsl:variable name="dec-doc" select="document($dec-file)"/> 

<xsl:output indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:key name="k1" match="TopLevel/*" use="name()"/> 

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

<xsl:template match="Level2[key('k1', LinkedTo, $dec-doc)/Available != 'Y']"/> 

<xsl:template match="Level2[key('k1', LinkedTo, $dec-doc)/Available = 'Y']/LinkedTo"/> 

</xsl:stylesheet> 
相關問題