2014-04-03 126 views
0

我是新來的XSLT和改造工作從一個XML到另一個跌破發行面臨添加節點到XML如果另一節點不存在

如果再書1點存在,我不應該看到的書2點到轉換的XML。我嘗試了不同的短語,但不起作用。

輸入XML

<?xml version="1.0" encoding="UTF-8"?> 
<Catalog> 
    <book1>Wise Otherwise</book1> 
    <book2>Great Expectations</book2>> 
</Catalog> 

預計XML 明智的,否則

下面是我的XSL

<?xml version="1.0" encoding="UTF-8"?> 

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

<xsl:if test="book1"> 
    <xsl:template match="book2" /> 
</xsl:if> 

<xsl:if test="not(book1)"> 
    <xsl:template match="book2"> 
    <book2> 
     <xsl:apply-templates /> 
    </book2> 
</xsl:template> 
</xsl:if> 
</xsl:stylesheet> 

我甚至嘗試的xsl:選擇

<xsl:choose> 
<xsl:when test="/catalog/book1"> 
    <xsl:template match="book2" /> 
</xsl:when> 
<!-- more xsl:when here, if needed --> 
<xsl:otherwise> 
    <xsl:template match="book2"> 
    <book2> 
     <xsl:apply-templates /> 
    </book2> 
    </xsl:template> 
</xsl:otherwise> 
</xsl:choose> 

第一冊和第二冊是從轉換後的XML相互排斥。

當且僅當Book1不在輸入XML中時,Book2應該處於已轉換的XML中。

我希望這會給你更好的畫面

感謝

回答

1

使用樣式表與身份轉換模板

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

和模板<xsl:template match="Catalog[Book-1]/Book-2"/>

根據您提供的XML樣本完整的XSLT樣式表

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

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

<xsl:template match="Catalog[book1]/book2"/> 

</xsl:stylesheet> 

,其將

<Catalog> 
    <book1>Wise Otherwise</book1> 
    <book2>Great Expectations</book2> 
</Catalog> 

<Catalog> 
    <book1>Wise Otherwise</book1> 

</Catalog> 
+0

欣賞你的迴應,但是這並沒有給我的期望結果<?xml version =「1.0」encoding =「UTF-8」?> \t \t \t的 \t \t \t \t \t的 \t \t \t \t \t的

+0

@ user3495155,請編輯你的問題,並把最小但完整的XML樣本,XSLT,想要的結果,你得到的結果,以便我們能夠理解和再現問題。在評論中閱讀代碼示例有點困難,我們也需要看到XML –

+0

你好馬丁,我已經更新了我的建議。 –

相關問題