在 XSLT 中,如何插入适用于不同模板的匹配标签
我对 XSLT 很陌生,只是还没有找到解决我的问题的方法。我有一个看起来像这样的 xml 文件(我无法改变这个 xml 的外观,意识到它有点奇怪):
<account>
<name>accountA</name>
</account>
<period>
<type>priormonth</type>
</period>
<period>
<type>currentmonth</type>
</period>
<account>
<name>accountB</name>
</account>
<period>
<type>priormonth</type>
</period>
<period>
<type>currentmonth</type>
</period>
xml 文件可以有可变数量的帐户/期间/期间数据集但它们总是按这个顺序。
我有一个 xsl 文件,如下所示:
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="account">
<name> <xsl:value-of select="name"/> </name>
</xsl:template>
<xsl:template match="period">
<type> <xsl:value-of select="type"/> </type>
</xsl:template>
上面的效果很好,因为它处理多个帐户/期间/期间的出现,我的输出如下:
<name>accountA</name>
<type>priormonth</type>
<type>currentmonth</type>
<name>accountB</name>
<type>priormonth</type>
<type>currentmonth</type>
但是,我想要插入一些附加标签,以便输出实际上看起来像:
<account>
<name>accountA</name>
<period>
<type>priormonth</type>
<type>currentmonth</type>
</period>
</account>
<account>
<name>accountB</name>
<period>
<type>priormonth</type>
<type>currentmonth</type>
</period>
</account>
有办法做到这一点吗?如果我的术语不太正确,请道歉。 谢谢
I'm very new to XSLT and just haven't been able to find a solution yet to my problem. I have an xml file that looks like (and I can't change the way this xml looks, realizing it's a bit odd):
<account>
<name>accountA</name>
</account>
<period>
<type>priormonth</type>
</period>
<period>
<type>currentmonth</type>
</period>
<account>
<name>accountB</name>
</account>
<period>
<type>priormonth</type>
</period>
<period>
<type>currentmonth</type>
</period>
The xml file can have a variable number of account/period/period datasets but they are always in that order.
I've got an xsl file which looks like:
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="account">
<name> <xsl:value-of select="name"/> </name>
</xsl:template>
<xsl:template match="period">
<type> <xsl:value-of select="type"/> </type>
</xsl:template>
The above works great because it deals with the multiple account/period/period occurrences and my output comes out like:
<name>accountA</name>
<type>priormonth</type>
<type>currentmonth</type>
<name>accountB</name>
<type>priormonth</type>
<type>currentmonth</type>
However, I'm wanting to have some additional tags inserted so that the output actually looks like:
<account>
<name>accountA</name>
<period>
<type>priormonth</type>
<type>currentmonth</type>
</period>
</account>
<account>
<name>accountB</name>
<period>
<type>priormonth</type>
<type>currentmonth</type>
</period>
</account>
Is there a way of doing this? Apologies if my terminology is not quite right.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
select 语句有点复杂,但它的基本作用是选择每个
account
元素后面的所有period
同级,其中第一个account
前面的元素是当前元素。Try this:
The select statement is a bit convoluted, but what it basically does it select all
period
siblings following eachaccount
element, where the firstaccount
element that precedes it is the current one.以下 XSLT 假定 xml 源与您的问题中所呈现的完全相同(除了缺少根导致源文档格式不正确)。在这种情况下,您不需要身份转换。此外,如果您的
帐户
确实只需要前两个句点
,您可以使用更简单的 XPath。XSLT 1.0 在 Saxon-HE 9.2.1.1J 下测试
应用于此输入:
给出:
The following XSLT assumes that the xml source is exactly as presented in your question (apart the missing root which makes the source document not well formed). In this case you do not need identity transform. Moreover, if really your
account
needs only the first next twoperiod
you can use a simpler XPath.XSLT 1.0 tested under Saxon-HE 9.2.1.1J
Applied on this input:
Gives: