0

在下面的示例中,我想用 替换<break/>标签<br />,或者至少返回所有文本(包括<break/>s),所以我可以<break/>用 Javascript 替换 s。

XML:

<?xml version='1.0' encoding='utf-8'?>
<document xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
          xsi:schemaLocation='http://www.lotus.com/dxl xmlschemas/domino_8_5_3.xsd'
          xmlns='http://www.lotus.com/dxl' >

<item name='item1'>
    <textlist>
        <text/>
    </textlist>
</item>
<item name='item2'>
    <textlist>
        <text>This<break/>is<break/>a<break/>broken<break/>sentence.<break/></text>
    </textlist>
</item>
</document>

编辑:我也只想返回 item2 的结果,完全忽略 item1。

4

1 回答 1

1

一个非常小的身份转换应该可以。
这里唯一的小“技巧”​​是考虑命名空间。试试这个:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:dxl='http://www.lotus.com/dxl'
                xmlns='http://www.lotus.com/dxl'

                 exclude-result-prefixes='dxl'  >
    <xsl:output method="xml" indent="yes"/>

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

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

</xsl:stylesheet>

这将生成以下输出:

<?xml version="1.0"?>
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.lotus.com/dxl" xsi:schemaLocation="http://www.lotus.com/dxl xmlschemas/domino_8_5_3.xsd">

    <item name="item1">
        <textlist>
            <text/>
        </textlist>
    </item>
    <item name="item2">
        <textlist>
            <text>
                This<br/>is<br/>a<br/>broken<br/>sentence.<br/>
            </text>
        </textlist>
    </item>
</document>

更新附加问题以忽略“item1” 在样式表中添加以下行:

<xsl:template match ="dxl:item[@name='item1']" />

或者如果只有“item2”应该在输出中,添加一个根模板:

<xsl:template match="/">
    <document>
        <xsl:apply-templates select="*/dxl:item[@name='item2']" />
    </document>
</xsl:template>
于 2013-05-16T14:36:20.560 回答