使用xslt合并/连接xml中属性的值



im卡在这一点:

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>

在将linkport节点合并到值为port和link的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>

注意

  1. <property>元素仅在代码中的一个位置构造,而与模板匹配的元素的名称无关。

  2. 该代码是通用的,并且没有明确提及诸如";CCD_ 2";或";CCD_ 3";

最新更新