2014-02-17 14 views
0

正在写一些XSLT脚本(1.0版本)句号,VS2010在XSLT如何找到如果文本包含在BizTalk映射到底

现在,在输入XML文件我有以下标签

<STUDENTS> 
<STUDENT>&lt;DETAILS NAME="Tuna"&gt;These are student. details. of Student1.&lt;/DETAILS&gt;</STUDENT> 
<STUDENT></STUDENT> 
</STUDENTS> 

现在,每一个上面,输出具有如下所示

<INFO NAME="Tuna">These are student. details. of Student1</INFO> 

现在用下面的脚本。

<xsl:for-each select="//STUDENTS/STUDENT"> 
<INFO> 
<xsl:attribute name="NAME"> 
    <xsl:value-of select="normalize-space(substring-before(substring-after(.,'NAME=&quot;'),'&quot;'))" /> 
    </xsl:attribute> 
    <xsl:variable name="replace1" select="normalize-space(substring-before(substring-after(.,'&gt;'),'&lt;/DETAILS&gt;'))" /> 
<xsl:value-of select="translate($replace1,'.','')"/> 
</INFO> 
</xsl:for-each> 

我的输出中看起来如下

<INFO NAME="Tuna">These are student details of "Student1" </INFO> 

但我只想要删除 “”这在最后出现。我怎么做?任何建议都非常感谢。

在此先感谢。

+0

[在XSLT串卸下的最后一个字符]的可能重复(http://stackoverflow.com/questions/1119449/removing -XSLT字符串中的最后字符) – Tomalak

回答

0

编辑请注意,这是一个XSLT 2.0的答案。如果它根本没用,我会删除它。

测试您的条件(.在字符串末尾)是否符合matches()函数和正则表达式。你会发现这个here小提琴。

如果matches()返回true,则输出排除最后一个字符的输入文本的子字符串。换句话说,它返回从第一个字符(索引1)开始并且长度为string-length() -1的子字符串$replace1

请注意,我冒昧地从样式表中删除xsl:for-each。在很多情况下使用模板是一种更好的方法。

样式

<?xml version="1.0" encoding="utf-8"?> 

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

    <xsl:output method="xml" indent="yes"/> 

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

    <xsl:template match="STUDENT"> 
     <INFO> 
     <xsl:attribute name="NAME"> 
      <xsl:value-of select="normalize-space(substring-before(substring-after(.,'NAME=&quot;'),'&quot;'))" /> 
     </xsl:attribute> 
     <xsl:variable name="replace1" select="normalize-space(substring-before(substring-after(.,'&gt;'),'&lt;/DETAILS&gt;'))" /> 

     <xsl:choose> 
      <xsl:when test="matches($replace1,'\.$')"> 
       <xsl:value-of select="substring($replace1,1,string-length($replace1)-1)"/> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="$replace1"/> 
      </xsl:otherwise> 
     </xsl:choose> 
     </INFO> 
    </xsl:template> 

</xsl:stylesheet> 

输出

<?xml version="1.0" encoding="UTF-8"?> 
<STUDENTS> 
    <INFO NAME="Tuna">These are student. details. of Student1</INFO> 
    <INFO NAME=""/> 
</STUDENTS> 
+0

问题要求XSLT 1.0。 –

+0

你是对的伊恩 - 对不起。我只是忽略了它。 –

1

正在写一些XSLT脚本(1.0版本)

如果使用XSLT 1.0,尝试类似:

<xsl:value-of select="substring($replace1, 1, string-length($replace1) - contains(concat($replace1, '§'), '.§'))"/> 

或者,优选:

<xsl:value-of select="substring($replace1, 1, string-length($replace1) - (substring($replace1, string-length($replace1), 1) = '.'))"/> 
+0

聪明,我喜欢它:-) –

+0

@IanRoberts哦,天哪。我相信你的意思是作为赞美,但我发誓我会停止做“聪明”和“可爱的伎俩”。显然我有一次复发。我会编辑我的答案,并发布一个不那么“聪明”和更直接的方法。 –