[英]Merge/concat values of an attribute in xml using xslt
我卡在这一点上:
XML源
<config>
<username>user</username>
<password>pass</password>
<link>1.2.3.4</link>
<port>99</port>
</config>
XML 目标
<some wrapping="tag"> <!-- is fixed and should be just inserted -->
<property name="username" value="user"/>
<property name="password" value="pass"/>
<!-- link and port are merged into one attribute -->
<property name="URL" value="1.2.3.4:99" />
</some>
XSLT 我到目前为止
<xsl:template match="config">
<some wrapping="tag">
<xsl:apply-templates />
</some>
<xsl:template>
<xsl:template match="config/username | config/password">
<property name="{local-name()}">
<xsl:attribute name="value">
<xsl:value-of select="." />
</xsl:attribute>
</property>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
它给了我想要的结果,直到将链接和端口节点合并到具有端口和链接值的URL属性中。
提前致谢!
怎么样:
<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="/config">
<some wrapping="tag">
<xsl:apply-templates select="username | password"/>
<property name="URL" value="{concat(link, ':', port)}" />
</some>
</xsl:template>
<xsl:template match="*">
<property name="{local-name()}">
<xsl:attribute name="value">
<xsl:value-of select="." />
</xsl:attribute>
</property>
</xsl:template>
</xsl:stylesheet>
要不就:
<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="/config">
<some wrapping="tag">
<xsl:for-each select="username | password">
<property name="{local-name()}">
<xsl:attribute name="value">
<xsl:value-of select="." />
</xsl:attribute>
</property>
</xsl:for-each>
<property name="URL" value="{concat(link, ':', port)}" />
</some>
</xsl:template>
</xsl:stylesheet>
如果您想要一个透明且易于阅读的解决方案,只需编写代码:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="config">
<some wrapping="tag">
<property name="username" value="{username}"/>
<property name="password" value="{password}"/>
<!-- link and port are merged into one attribute -->
<property name="URL" value="{link}:{port}" />
</some>
</xsl:template>
</xsl:stylesheet>
更短:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*" priority="10">
<some wrapping="tag">
<xsl:apply-templates/>
</some>
</xsl:template>
<xsl:template match="*[not(self::port)]">
<property name="{concat(substring(name(), string-length(name())*(name()='link') +1),
substring('URL', 3*(not(name()='link')) +1))}"
value="{concat(.,substring(concat(':',../port),
(string-length(../port)+1)*not(name()='link')+1) )}"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
请注意:
<property>
元素仅在代码中的一个位置构造,与模板匹配的元素名称无关。
代码是通用的,没有明确提及元素名称,例如“ username
”或“ password
”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.