xsl:apply-templates,带有元素替换

时间:2012-08-22 18:45:29

标签: xslt-2.0

非常感谢您提供的任何建议。我正在努力将XML文档的一部分转换为列表。我有大部分转型工作;但是,我被挂在<emph>子元素上。我想用“(引用)替换它,但我无法解决替换策略。

干杯! XML:

<ead>
<archdesc>
    <dsc>
        <head>Container List</head>
        <c01 id="ref1251" level="file">
            <did>
                <unittitle>#464: Dutch. <emph render="doublequote">I know Mary [Frances <emph
                            render="doublequote">Dutchess</emph> (Watrous) Roth] packed it some
                        where!</emph>,</unittitle>
                <container id="cid4822615" type="Box" label="Mixed Materials">2</container>
                <container parent="cid4822615" type="Folder">110</container>
                <unitdate>undated</unitdate>
                <physdesc id="ref1252" label="General Physical Description note"
                    >(Negative)</physdesc>
            </did>
        </c01>
        <c01 id="ref1331" level="file">
            <did>
                <unittitle>#476: Mountain home near Cosby,</unittitle>
                <container id="cid4822586" type="Box" label="Mixed Materials">2</container>
                <container parent="cid4822586" type="Folder">139</container>
                <unitdate>undated</unitdate>
                <physdesc id="ref1332" label="General Physical Description note">(6 of
                    6)</physdesc>
                <physdesc id="ref1333" label="General Physical Description note"
                    >(Print)</physdesc>
            </did>
        </c01>
    </dsc>
</archdesc>
</ead>

XSLT:

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

<xsl:output method="text"/>
<xsl:strip-space elements="*"/>


<xsl:template match="/">
    <xsl:apply-templates select="ead/archdesc/dsc"/>
</xsl:template>

<xsl:template match="dsc">
    <xsl:apply-templates select="c01/did"/>
</xsl:template>

<xsl:template match="did">
    <xsl:apply-templates select="unittitle"/>
    <xsl:text>&#09;</xsl:text>
    <xsl:text>Box: </xsl:text><xsl:apply-templates select="container[1]"/>
    <xsl:text>&#09;</xsl:text>
    <xsl:text>Folder: </xsl:text><xsl:apply-templates select="container[2]"/>
    <xsl:text>&#09;</xsl:text>
    <xsl:apply-templates select="unitdate"/>
    <xsl:text>&#09;</xsl:text>
    <xsl:apply-templates select="physdesc"/>
    <xsl:text>&#10;</xsl:text>
</xsl:template>

1 个答案:

答案 0 :(得分:1)

以下是一个如何完成此操作的示例(在XSLT 2.0和XSLT 1.0中):

<xsl:stylesheet version="2.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="emph[@render eq 'doublequote']">
     <xsl:text>"</xsl:text>
     <xsl:apply-templates/>
     <xsl:text>"</xsl:text>
 </xsl:template>
</xsl:stylesheet>

将此转换应用于以下简短XML文档(摘自提供的文档):

<unittitle>#464: Dutch. 
    <emph render="doublequote">I know Mary [Frances 
        <emph render="doublequote">Dutchess</emph> (Watrous) Roth] packed it some
                        where!</emph>,
</unittitle>

产生了想要的正确结果

#464: Dutch. 
    "I know Mary [Frances 
        "Dutchess" (Watrous) Roth] packed it some
                        where!",