EXSLT str:替换

时间:2012-04-17 15:27:55

标签: xslt exslt

我在使用EXSLT str正确转换XML时遇到问题:替换模板。

这是我的XML:

<XML>
<Item>
    <Field>
        <Name>ID</Name>
        <Value>98765</Value>
    </Field>
    <Field>
        <Name>ParentID</Name>
        <Value>10002</Value>
    </Field>
    <Field>
        <Name>Type</Name>
        <Value>content</Value>
    </Field>
    <Field>
        <Name>Name</Name>
        <Value>TestAPI Course</Value>
    </Field>
    <Field>
        <Name>URL</Name>
        <Value></Value>
    </Field>
    <Field>
        <Name>Description</Name>
        <Value>a description</Value>
    </Field>
    <Field>
        <Name>DateBegin</Name>
        <Value>2012-04-04T00:00:00.000-07:00</Value>
    </Field>
    <Field>
        <Name>DateEnd</Name>
        <Value>2012-04-04T00:00:00.000-08:00</Value>
    </Field>
    <Field>
        <Name>DateCreated</Name>
        <Value>2012-04-03T00:00:00.000-07:00</Value>
    </Field>
    <Field>
        <Name>DateModified</Name>
        <Value>2012-04-04T00:00:00.000-06:00</Value>
    </Field>
</Item>

这是我的XSL:

<?xml version="1.0" encoding="utf-16"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings" extension-element-prefixes="str" version="1.0">
        <xsl:import href="C:\inetpub\wwwroot\LMSConnector\LMS\XSL\str.xsl"/>
        <xsl:template match="/Items">
            <xsl:call-template name="str:replace">
                <xsl:with-param name="string" select="Field/Name"/>
                <xsl:with-param name="search" select="ID"/>
                <xsl:with-param name="replace" select="sco-id"/>
            </xsl:call-template>
        </xsl:template>
    </xsl:stylesheet>

我的问题是我只获得了一个XML文档标题而没有其他内容,但没有其他内容?我不认为我远离一个有效的解决方案,问题可能在于我为模板上的“match”参数设置的值,以及call-template with-param节点中的select参数。

任何帮助都将不胜感激。

麦克

1 个答案:

答案 0 :(得分:1)

您尝试匹配<xsl:template match="/Items">,但您的XML包含<Item>个元素,而不是<Items>

修改


这是你想要做的吗?

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings" extension-element-prefixes="str">
    <xsl:import href="http://delicious-library-export.googlecode.com/svn/trunk/str/str.xsl"/>
    <xsl:output method="xml" encoding="utf-8" indent="no"/>

    <xsl:template match="/Item/Field/Name/text()">
        <xsl:call-template name="str:replace">
          <xsl:with-param name="string" select="."/>
          <xsl:with-param name="search" select="'ID'"/>
          <xsl:with-param name="replace" select="'sco-id'"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="*">      
      <xsl:copy>
        <xsl:apply-templates/>
      </xsl:copy>      
    </xsl:template>

    </xsl:stylesheet>

结果:

<?xml version="1.0" encoding="utf-8"?>
<Item>
    <Field>
        <Name>sco-id</Name>
        <Value>98765</Value>
    </Field>
    <Field>
        <Name>Parentsco-id</Name>
        <Value>10002</Value>
    </Field>
    <Field>
        <Name>Type</Name>
        <Value>content</Value>
    </Field>
    <Field>
        <Name>Name</Name>
        <Value>TestAPI Course</Value>
    </Field>
    <Field>
        <Name>URL</Name>
        <Value/>
    </Field>
    <Field>
        <Name>Description</Name>
        <Value>a description</Value>
    </Field>
    <Field>
        <Name>DateBegin</Name>
        <Value>2012-04-04T00:00:00.000-07:00</Value>
    </Field>
    <Field>
        <Name>DateEnd</Name>
        <Value>2012-04-04T00:00:00.000-08:00</Value>
    </Field>
    <Field>
        <Name>DateCreated</Name>
        <Value>2012-04-03T00:00:00.000-07:00</Value>
    </Field>
    <Field>
        <Name>DateModified</Name>
        <Value>2012-04-04T00:00:00.000-06:00</Value>
    </Field>
</Item>