插入基于另一元件

问题描述:

我有这样插入基于另一元件

<document startnum="1" language="en-US"> 
<meta> 
<categories> 
<category medicalbranch="surgery"> 
</category> 
</categories> 
<doctitle>Traumatology for the Physical Therapist</doctitle> 
<relatedobjects> 
<relpdfio/> 
</relatedobjects> 
<publisher> 
<publishername>Georg Thieme Verlag</publishername> 
<copyright>Georg Thieme Verlag</copyright> 
</publisher> 
</meta> 

一个输入XML文件上输出XML应该像下面

<document startnum="1" language="en-US"> 
<meta> 
<categories> 
<category medicalbranch="surgery"> 
</category> 
</categories> 
<isbn type="print"> </isbn> 
<isbn type="online"> </isbn> 
<materialid/> 
<metadata type="searchlevel"/> 
<doctitle>Traumatology for the Physical Therapist</doctitle> 
<relatedobjects> 
<relpdfio/> 
</relatedobjects> 
<publisher> 
<publishername>Georg Thieme Verlag</publishername> 
<copyright>Georg Thieme Verlag</copyright> 
</publisher> 
</meta> 

我需要上述插入提到4个元件每当“新元素meta“元素在源代码中不包含”isbn“。

我已经写了XSLT象下面这样:

<xsl:template match="meta"> 
    <xsl:if test="node[not(isbn)]"> 
     <xsl:element name="isbn"> 
      <xsl:attribute name="type">print</xsl:attribute> 
      <xsl:text>&#xA0;</xsl:text> 
     </xsl:element><xsl:text>&#xa;</xsl:text> 
     <xsl:element name="isbn"> 
      <xsl:attribute name="type">online</xsl:attribute> 
      <xsl:text>&#xA0;</xsl:text> 
     </xsl:element> 
     <xsl:text>&#xa;</xsl:text>   
     <xsl:element name="materialid"></xsl:element><xsl:text>&#xa;</xsl:text> 
     <xsl:element name="metadata"> 
      <xsl:attribute name="type">searchlevel</xsl:attribute></xsl:element> 
     <xsl:text>&#xa;</xsl:text> 
     <!--<xsl:text disable-output-escaping="yes">-\-&gt;</xsl:text>--> 
     <xsl:apply-templates select="@*|node()"/> 
     <xsl:text>&#xa;</xsl:text> 
    </xsl:if> 
    <xsl:copy> 
     <xsl:apply-templates select="node() | @*"/> 
    </xsl:copy> 
</xsl:template> 

能否请您帮助我们解决这个问题。

开始与身份转换模板

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

,然后决定你想要的categories元素之后插入新的元素,例如如

<xsl:template match="meta[not(isbn)]/categories"> 
    <xsl:next-match/><!-- copies the categories element with the identity transformation template --> 
    <isbn type="print"> </isbn> 
    <isbn type="online"> </isbn> 
    <materialid/> 
    <metadata type="searchlevel"/> 
</xsl:template> 
+0

感谢您的回应Martin。它在我的文件中工作。 – Sumathi

+0

我想保持自我结束标签,如。你能帮助我,如何为它编写代码。 – Sumathi

+0

XSLT没有定义空元素是否被序列化为例如''或''所以我恐怕这是XSLT以外的任务。在http://xsltransform.net/3MvmrAL中,您的示例输入似乎可以通过''结果正常转换,但通常取决于XSLT处理器/序列化程序。 –