在使用XSLT进行复制时,如何将模板的结果(最后一个标记)用作特定属性的值



我正在尝试复制XML文件并更改(特定(属性(FilePath(的值。属性的新值应该是最后一个反斜杠之后的最后一个字符串。所以我使用了最后一个令牌模板,然后将其保存在一个变量"中;结果";。我总是得到空值,我称之为";最后一个标记";两次我是一名学生,我不熟悉XSLT,这是一个大学项目,请帮忙!!感谢

我的XML:

<Document>
<Field Type="DD_FilePath" Value="C:UsersukaDesktop0000002.jpg"/>
<Field Type="aaa" Value= "3"/>

</Document>

XML应该如何转换:

<Document>
<Field Type="DD_FilePath" Value="00000002.jpg"/>
<Field Type="aaa" Value= "3"/>

</Document>

我的XSLT:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>


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

</xsl:template>
<xsl:template match="Field[@Type='DD_FilePath']/@Value">
<xsl:variable name="result">

<xsl:apply-templates select="last-token"/> 


</xsl:variable>
<xsl:value-of select="$result" />
<xsl:attribute name="Value">

<xsl:value-of select="$result"/>
</xsl:attribute>
</xsl:template> 

<xsl:template name="last-token">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="''"/>

<xsl:choose>
<xsl:when test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="last-token">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>

</xsl:template>   
</xsl:stylesheet>

命名模板需要被调用,而不是应用-通常带有参数。代替:

<xsl:template match="Field[@Type='DD_FilePath']/@Value">
<xsl:variable name="result">

<xsl:apply-templates select="last-token"/> 


</xsl:variable>
<xsl:value-of select="$result" />
<xsl:attribute name="Value">

<xsl:value-of select="$result"/>
</xsl:attribute>
</xsl:template> 

do:

<xsl:template match="Field[@Type='DD_FilePath']/@Value">
<xsl:variable name="result">
<xsl:call-template name="last-token">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:variable>
<xsl:attribute name="Value">
<xsl:value-of select="$result"/>
</xsl:attribute>
</xsl:template> 

或者简单地说:

<xsl:template match="Field[@Type='DD_FilePath']/@Value">
<xsl:attribute name="Value">
<xsl:call-template name="last-token">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:attribute>
</xsl:template> 

最新更新