2014-04-24 77 views
0

我想一个XML文件转换到另一个XML文件,在一些情况下,添加了额外内容不提供内容添加到XML。XSLT:根据条件

我的XML看起来是这样的:

<bookstore> 
    <book> 
     <title>Book1</title> 
     <author>Author1</author> 
     <chapters> 
      <chapter> 
        <ch-name>BLABLA</ch-name> 
        <ch-number>1</ch-number> 
      </chapter> 
     </chapters> 
    </book> 
    <book> 
     <title>Book2</title> 
     <author>Author2</author> 
     <chapters> 
      <chapter> 
        <ch-name>Test</ch-name> 
        <ch-number>1</ch-number> 
      </chapter> 
     </chapters> 
    </book> 
    <book> 
     <title>Book3</title> 
     <author>Author3</author> 
    </book> 
</bookstore> 

现在,我要的章节元素(及其子元素)添加到书籍,它不存在,创建某种违约。

我的XSLT是这样的:

<dns:template match="book[not(exists(chapters))]"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|*"/> 
    </xsl:copy> 
    <chapters> 
      <chapter> 
       <ch-name>default</ch-name> 
       <ch-number>default</ch-number> 
      </chapter> 
    </chapters> 
</dns:template> 

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

然而,当我尝试这一个在线编辑器,没有任何更改。 dns命名空间应该支持not(exists)部分。

有什么错我的XSLT?

感谢您给予的帮助!

+0

'dns:template'?你的意思是'xsl:template'? –

+0

没有因为那时我不能使用没有(存在)的条款,可以吗?他们在另一个命名空间 – Tcanarchy

回答

2

更改“DNS”前缀“XSL”(当然你也可以有相同的命名空间再次定义的前缀,但不推荐)。如果您使用XSLT1.0,则不存在“exists()”函数。 另外,“章节”需要在里面。 您可以使用此xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes"/> 
<xsl:strip-space elements="*"/> 
<xsl:template match="book[not(chapters)]"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|*"/> 
     <chapters> 
      <chapter> 
       <ch-name>default</ch-name> 
       <ch-number>default</ch-number> 
      </chapter> 
     </chapters> 
    </xsl:copy> 

</xsl:template> 

<xsl:template match="@* | *"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | * | text()"/> 
    </xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 
+0

谢谢您指定的,问题是事实,章节是外面的xsl:副本。 – Tcanarchy