使用XSL将元素添加到XML

时间:2016-06-27 13:48:07

标签: xml xslt lotus-notes

这是我从Lotus Notes数据库导出的XML:

<?xml version="1.0" encoding="UTF-8"?>
<database xmlns="http://www.lotus.com/dxl" path="CN=ATRVSIC1/OU=RVS/O=Raiffeisen Sbg.!!notesent\frzld\test.nsf" type="standard" title="test" uselz1="true" version="8.5" nourlopen="false" replicaid="C1257D2B002A37BB" requiressl="false" multilingual="false" maintainunread="true" multidbindexed="false" logtransactions="true" advancedtemplate="false" allowstoredforms="true" savelastaccessed="false" showinopendialog="true" allowsoftdeletion="false" increasemaxfields="false" allowdesignlocking="false" imageloadsdeferred="true" maintenanceversion="2.4" markmodifiedunread="true" overwritefreespace="false" singlecopytemplate="false" allowdocumentlocking="false" optimizetablebitmaps="false" usejavascriptinpages="true" allowbackgroundagents="true" allowheadlinemonitors="true" allowspecialhierarchy="true" unicodestandardsorting="false" allowexternalconnections="false" copyprofiledocswithdesign="false">
<databaseinfo dbid="C1257D2B002A37BB" diskspace="53215232" odsversion="43" percentused="11.0086976600985" numberofdocuments="52"><datamodified><datetime dst="true">20160623T092148,11+02</datetime></datamodified><designmodified><datetime dst="true">20160624T081402,68+02</datetime></designmodified></databaseinfo>
<fulltextsettings size="11670496" update="immediate" breakat="words" casesensitive="false" attachmenttype="rawtextonly" includeattachments="false" includeencryptedfields="true"><lastindexed><datetime dst="false">20160623T081159,12+00</datetime></lastindexed></fulltextsettings>
<launchsettings><noteslaunch frameset="Main" whenopened="openframeset"/></launchsettings>
<form name="DynamicFields" type="document" recalc="false" default="false" inherit="false" private="false" mailable="false" versions="none" anonymous="false" noreplace="false" storeform="false" useapplet="false" editonopen="false" inheritdoc="none" contextpane="hide" nofocusonf6="false" treatashtml="false" nofieldindex="false" publicaccess="false" readingorder="lefttoright" allowautosave="false" manualversion="false" signdocuments="false" conflictaction="createconflicts" noinitialfocus="false" designerversion="8.5.2" nofieldexchange="false" renderpassthrough="true">
<noteinfo unid="F6A625845C3510AAC1257FDB001A6F04" noteid="34e" sequence="14">
<created><datetime dst="true">20160623T064843,56+02</datetime></created>
<modified><datetime dst="true">20160624T070923,11+02</datetime></modified>
<revised><datetime dst="true">20160624T070923,10+02</datetime></revised>
<lastaccessed><datetime dst="true">20160624T070923,11+02</datetime></lastaccessed>
<addedtofile><datetime dst="true">20160623T064843,85+02</datetime></addedtofile></noteinfo>
<updatedby><name>CN=Daniel Lesiak/OU=RVS/O=Raiffeisen Sbg.</name></updatedby>
<wassignedby><name>CN=Daniel Lesiak/OU=RVS/O=Raiffeisen Sbg.</name></wassignedby>
<globals><code event="options"><lotusscript>Option Public
Use "DynamicFields"

</lotusscript></code></globals>
<actionbar align="left" bgcolor="#f0f0f0" bordercolor="black" useappletinbrowser="false" showdefaultrightclickitems="true">
<actionbuttonstyle bgcolor="#f0f0f0" textalign="left" widthtype="default" heighttype="default" margintype="default" showdropdown="false" displayborder="onmouseover" internalmargin="0"/><font color="system"/><border color="black" style="solid" width="0px 0px 1px" dropshadow="false" insidewidth="0" outsidewidth="0" dropshadowwidth="12px"/>
<action hide="" align="left" title="Speichern" showinbar="true" showinmenu="true" displaytype="button" graphicalign="left" readingorder="lefttoright" onlyiconinbar="false" publishonolelaunch="false" showinrightbuttonmenu="false"><code event="click"><formula>@Command([FileSave])</formula></code></action></actionbar>
<body><richtext>
<pardef id="1" hide="" list="none" align="left" newpage="false" spaceafter="1" linespacing="1" spacebefore="1" readingorder="lefttoright"/>
<par def="1"/>
<par def="1"><button edge="system" type="normal" width="2in" bgcolor="system" default="false" maxlines="1" wraptext="true" widthtype="maximum" readingorder="lefttoright">Create Field<code event="click"><lotusscript>Sub Click(Source As Button)
    Dim w As New NotesUIWorkspace

    Call CreateField(w.CurrentDocument.Document, "Daniel")
End Sub</lotusscript></code></button></par>
<par def="1"><field kind="editable" name="Herbert" sign="false" type="text" protected="false" allowtabout="false" defaultfocus="false" storelocally="false" showdelimiters="true" allowmultivalues="false" useappletinbrowser="false" dataconnectionfield="data" computeaftervalidation="false"/><compositedata type="98" nexttype="222" prevtype="65418" afterparcount="6" containertype="65418" afterbegincount="3" aftercontainercount="1">
Yg4BAIQAAAAAAAAAAAA=
</compositedata></par>
<par def="1"><field kind="computed" name="Sieglinde" sign="false" type="text" protected="false" allowtabout="false" defaultfocus="false" storelocally="false" showdelimiters="true" allowmultivalues="false" useappletinbrowser="false" dataconnectionfield="data" computeaftervalidation="false"><code event="defaultvalue"><formula>"Sieglinde"</formula></code></field><compositedata type="98" nexttype="222" prevtype="65418" afterparcount="6" containertype="65418" afterbegincount="3" aftercontainercount="1">
Yg4BAIQAAAAAAAAAAAA=
</compositedata></par>
<par def="1"/></richtext></body>
<item name="$$ScriptName" seal="false" sign="true" names="false" sealed="false" authors="false" readers="false" summary="false" placeholder="false"><text>DynamicFields</text></item></form>
</database>

看起来非常混乱,但这就是我如何导出它。现在我想做的是添加另一个&#34; par&#34;元素&#34; pardef&#34;使用XSL文件。这是我的方法:

<?xml version='1.0' encoding='utf-8'?>
<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="database/form/body/richtext/pardef">
        <xsl:copy-of select="."/>
        <xsl:element name="par">
            <xsl:attribute name="def">2</xsl:attribute>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

但是,除了复制已经存在的XML之外,这似乎并没有做任何事情。我添加其他元素的模板并不适用。 为什么不适用,我怎样才能得到我想要的结果呢?

1 个答案:

答案 0 :(得分:2)

这是命名空间问题。就在您的XML开头就有了这个......

 <database xmlns="http://www.lotus.com/dxl" 

这意味着database元素和所有后代元素都在此命名空间中。名称空间中的元素(如pardef)与不在空格中的元素不同,即使它也称为pardef

要处理这个问题,您只需要在XSLT中声明和使用命名空间。

试试这个XSLT

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

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

    <xsl:template match="dxl:database/dxl:form/dxl:body/dxl:richtext/dxl:pardef">
        <xsl:copy-of select="."/>
        <xsl:element name="par" namespace="http://www.lotus.com/dxl">
            <xsl:attribute name="def">2</xsl:attribute>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

请注意,使用的前缀(在这种情况下为dxl)并不重要。它是XML和XSLT之间必须匹配的名称空间URI(http://www.lotus.com/dxl)。

请注意,您可以将模板匹配更改为<xsl:template match="dxl:pardef">。您不需要指定完整路径,除非有pardef个元素是您不想转换的不同级别。

另请注意,您也可以将<xsl:element name="par" namespace="http://www.lotus.com/dxl">更改为此....

<dxl:par test="2" />