[英]Transform two nodes into one using XSLT1.0
Input: 输入:
<cp>
...
<conEmail>eeee</conEmail>
<conPhone>1800<conPhone>
...
</cp>
Output: 输出:
<cp>
...
<con>
<email>eeee</email>
<phone>1800</phone>
</con>
...
</cp>
Xsl-t sl
<xsl:template match="*[starts-with(name(), 'con')]">
<xsl:element name="con">
<xsl:element name="email">
<xsl:value-of select="." />
</xsl:element>
<xsl:element name="phone">
<xsl:value-of select="following-sibling::*[1]"></xsl:value-of>
</xsl:element>
</xsl:element>
</xsl:template>
I tried to match with *[starts-with(name(), 'con')]
but then its matching the nodes twice... Any help? 我试图与*[starts-with(name(), 'con')]
进行匹配,但随后两次与节点匹配……有什么帮助吗?
It is difficult to answer a question that contains only snippets, because the overall context is important. 很难回答仅包含摘要的问题,因为总体情况很重要。
The following stylesheet: 以下样式表:
XSLT 1.0 XSLT 1.0
<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="/cp">
<xsl:copy>
<!-- other things? -->
<con>
<xsl:apply-templates select="*[starts-with(name(), 'con')]"/>
</con>
<!-- more of other things? -->
</xsl:copy>
</xsl:template>
<xsl:template match="*[starts-with(name(), 'con')]">
<xsl:element name="{substring-after(local-name(),'con')}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
when applied to your input (after correcting the unclosed conPhone
tag!), will return: 当应用于您的输入(更正未关闭的conPhone
标签!)后,将返回:
<?xml version="1.0" encoding="utf-8"?>
<cp>
<con>
<Email>eeee</Email>
<Phone>1800</Phone>
</con>
</cp>
If you know in advance the names of the "con*" elements, it would be better to handle them explicitly, for example: 如果事先知道“ con *”元素的名称,则最好显式地处理它们,例如:
<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="/cp">
<xsl:copy>
<!-- other things? -->
<con>
<xsl:apply-templates select="conEmail | conPhone"/>
</con>
<!-- more of other things? -->
</xsl:copy>
</xsl:template>
<xsl:template match="conEmail">
<email>
<xsl:value-of select="." />
</email>
</xsl:template>
<xsl:template match="conPhone">
<phone>
<xsl:value-of select="." />
</phone>
</xsl:template>
</xsl:stylesheet>
to return: 返回:
<?xml version="1.0" encoding="utf-8"?>
<cp>
<con>
<email>eeee</email>
<phone>1800</phone>
</con>
</cp>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.