XSLT转换、字符串连接
我有下面的xmlXSLT转换、字符串连接,xslt,string-concatenation,Xslt,String Concatenation,我有下面的xml <R N="14" MIME="application/pdf"> <RK>7</RK> <MT N="Abstract" V="Lorem Ipsum is simply dummy text of the printing " /> <MT N="Abstract1" V="and typesetting industry. Lorem Ipsum has been the i
<R N="14" MIME="application/pdf">
<RK>7</RK>
<MT N="Abstract"
V="Lorem Ipsum is simply dummy text of the printing " />
<MT N="Abstract1"
V="and typesetting industry. Lorem Ipsum has been the industry's standard "/>
<MT N="Author" V="Bernard Shaw;" />
<MT N="Author1" V="Mark Twain" />
<MT N="Abstract2"
V="dummy text ever since the 1500s, when an unknown printer took a galley"/>
<LANG>en</LANG>
</R>
抽象、抽象1、抽象2可以在xml中以任何顺序出现
我试着使用下面这样的东西,但是停留在一个条件上&当抽象时字符串的串联,抽象1不以相同的顺序出现
<xsl:template match="MT">
<xsl:if test="(some generic condition to display Title)">
<br/>
<span class="f">
<xsl:value-of select="@N"/>
</span>
</xsl:if>
<xsl:value-of select="@V"/>
</xsl:template>
感谢您的帮助。此转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
Abstract: <xsl:text/>
<xsl:for-each select="*/MT[starts-with(@N, 'Abstract')]">
<xsl:value-of select="@V"/>
</xsl:for-each>
Author: <xsl:text/>
<xsl:for-each select="*/MT[starts-with(@N, 'Author')]">
<xsl:value-of select="@V"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
Abstract: <xsl:text/>
<xsl:call-template name="concatAttributes"/>
Author: <xsl:text/>
<xsl:call-template name="concatAttributes">
<xsl:with-param name="pKeyStartString" select="'Author'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="concatAttributes">
<xsl:param name="pKeyAttribName" select="'N'"/>
<xsl:param name="pKeyStartString" select="'Abstract'"/>
<xsl:param name="pValueAttribName" select="'V'"/>
<xsl:for-each select=
"*/MT[starts-with(@*[name()=$pKeyAttribName], $pKeyStartString)]">
<xsl:value-of select="@*[name()=$pValueAttribName]"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
进一步重构:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
Abstract: <xsl:text/>
<xsl:for-each select="*/MT[starts-with(@N, 'Abstract')]">
<xsl:value-of select="@V"/>
</xsl:for-each>
Author: <xsl:text/>
<xsl:for-each select="*/MT[starts-with(@N, 'Author')]">
<xsl:value-of select="@V"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
Abstract: <xsl:text/>
<xsl:call-template name="concatAttributes"/>
Author: <xsl:text/>
<xsl:call-template name="concatAttributes">
<xsl:with-param name="pKeyStartString" select="'Author'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="concatAttributes">
<xsl:param name="pKeyAttribName" select="'N'"/>
<xsl:param name="pKeyStartString" select="'Abstract'"/>
<xsl:param name="pValueAttribName" select="'V'"/>
<xsl:for-each select=
"*/MT[starts-with(@*[name()=$pKeyAttribName], $pKeyStartString)]">
<xsl:value-of select="@*[name()=$pValueAttribName]"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
摘要:
作者:
第二次重构(OP要求):
<R N="14" MIME="application/pdf">
<RK>7</RK>
<MT N="Abstract" V="Lorem Ipsum is simply dummy text of the printing " />
<MT N="Abstract1" V="and typesetting industry. Lorem Ipsum has been the industry's standard " />
<MT N="Author" V="Bernard Shaw;" />
<MT N="Author1" V="Mark Twain" />
<MT N="Abstract2" V="dummy text ever since the 1500s, when an unknown printer took a galley" />
<LANG>en</LANG>
</R>
Abstract: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley
Author: Bernard Shaw;Mark Twain
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vAbstract">
<xsl:apply-templates mode="retrieve" select="//MT"/>
</xsl:variable>
<xsl:variable name="vAuthors">
<xsl:apply-templates mode="retrieve" select="//MT">
<xsl:with-param name="pKeyStartString" select="'Author'"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:template match="/">
Abstract: <xsl:value-of select="$vAbstract"/>
Authors:: <xsl:value-of select="$vAuthors"/>
</xsl:template>
<xsl:template match="MT" mode="retrieve">
<xsl:param name="pKeyAttribName" select="'N'"/>
<xsl:param name="pKeyStartString" select="'Abstract'"/>
<xsl:param name="pValueAttribName" select="'V'"/>
<xsl:if test="starts-with(@*[name()=$pKeyAttribName], $pKeyStartString)">
<xsl:value-of select="@*[name()=$pValueAttribName]"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
摘要:
作者::
编辑:OP要求“所有处理应在模板匹配的MT
内完成”。虽然这在简单的情况下是可能的(参见@Alejandro的答案),但在单个模板中进行所有处理匹配MT
会打开很大的未知空白。例如,可能需要以不同的方式处理其他MT
元素,在这种情况下,根本不会进行此类处理
在更复杂的情况下(例如,当元素和属性以任何顺序出现,但输出必须排序--Abstract1、Abstract2、…、Abstract-N),则必须显式指定排序,并且必须在模板匹配MT
之外因此,在一般情况下,仅在模板匹配MT
我建议将与MT
匹配的单个模板设置为命名模式,并在调用方明确应用的“pull样式”中使用。
<xsl:for-each select="MT[starts-with(@N, 'Abstract')]">
<xsl:if test="position() = 1">
<xsl:value-of select="@N"/>:
</xsl:if>
<xsl:value-of select="@V"/>
</xsl:for-each>
:
此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="kMtByAlphaN"
match="MT"
use="concat(generate-id(..),'+',translate(@N,'1234567890',''))"/>
<xsl:template match="MT[count(.|key('kMtByAlphaN',
concat(
generate-id(..),'+',
translate(@N,'1234567890','')
)
)[1]
) = 1
]">
<xsl:variable name="vName"
select="translate(@N,'1234567890','')"/>
<xsl:value-of select="concat($vName,': ')"/>
<xsl:apply-templates select="key('kMtByAlphaN',
concat(generate-id(..),'+',$vName)
)/@V"/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
注意:按@N字母内容和父级生成的id进行分组,因为我希望有几个
R
元素。如果您能给出示例输出(您看到的内容和希望看到的内容),那将非常有帮助。好问题,+1。请参阅我的答案,以获得一个完整、简短且简单的解决方案,该解决方案经过重构,最具通用性。:)检查我的答案,了解分组方法,只需更改规则匹配MT
元素。谢谢Dimitre。我在找更通用的东西。也就是说,可能还有很多其他标签,比如Abstract和Author。它们都将遵循以下格式-name、name1、name2、name3。。。。这些需要显示为name:value of(name+name1+..+nameN)@itsbalur:See my refactoring.)@迪米特里,再次感谢你。我还有一个限制。匹配必须是
,因为我试图做的是更大的XSLT的一部分。您能告诉我,要使上述代码在这种限制下工作,需要更改哪些内容吗?@itsbalur:这不可能仅在模板内部实现匹配MT
。如果这样做,文本“Abstract:”
将出现N次,但您只需要一次。@itsbalur:生成了一个满足您要求的“第二次重构”。