我的要求是,以下面的源XML转换成目标XML。能否请你帮我所需要的XSLT?
中以r为XML:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<property><namevalue name="loccode1">BIN01</namevalue></property>
<property><namevalue name="locdesc1">Description for BIN01</namevalue></property>
<property><namevalue name="loccode2">BIN02</namevalue></property>
<property><namevalue name="locdesc2">Description for BIN02</namevalue></property>
<property><namevalue name="loccode3">BIN03</namevalue></property>
<property><namevalue name="locdesc3">Description for BIN03</namevalue></property>
</catalog>
目标XML:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<property>
<namevalue name="location1">BIN01#Description for BIN01</namevalue>
</property>
<property>
<namevalue name="location2">BIN02#Description for BIN02</namevalue>
</property>
<property>
<namevalue name="location3">BIN03#Description for BIN03</namevalue>
</property>
</catalog>
的要求是具有属性namevalue
和loccode<seq>
来连接locdesc<seq>
节点的值。能否请你帮我实现这一目标?
下面的代码片段
<xsl:apply-templates select="catalog/property/namevalue[@name='loccode2'] | catalog/property/namevalue[@name='locdesc2']" />
期待我明确指定属性名称和输出BIN01Description for BIN01
(我甚至无法添加一个分隔符)。有动态传递属性(e.g loccode<seq>
或locdesc<seq>
),并附加复制的文本之间的分隔符的方式。
有动态传递属性(e.g loccode或locdesc)的方式
是。你可以使用:
/catalog/property[starts-with(namevalue/@name, 'loccode')]
只有选择location codes
性能。
另外,如果结构遵循一个规律,你可以使用:
/catalog/property[position() mod 2 = 1]
也作出了同样的选择,然后:
following-sibling::property[1]
选择相邻location description
属性。
您还可以使用此作为替代方案:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="property[namevalue[contains(@name, 'loccode')]]">
<property>
<xsl:for-each select="namevalue[contains(@name, 'loccode')]">
<xsl:variable name="seq" select="concat('locdesc', substring-after(@name, 'loccode'))"/>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:value-of select="concat(., '#', //namevalue[@name = $seq])"/>
</xsl:copy>
</xsl:for-each>
</property>
</xsl:template>
<xsl:template match="property[namevalue[contains(@name, 'locdesc')]]"/>
</xsl:stylesheet>
<xsl:template match="catalog">
<catalog>
<xsl:apply-templates select="property"/>
</catalog>
</xsl:template>
<xsl:template match="property/namevalue">
<xsl:choose>
<xsl:when test="contains(., 'Description for ')"/>
<xsl:otherwise>
<property>
<namevalue>
<xsl:if test="@name">
<xsl:attribute name="name">
<xsl:value-of select="concat('location', substring-after(@name, 'loccode'))"/>
</xsl:attribute>
<xsl:value-of select="concat(.,'#','Description for ',.)"/>
</xsl:if>
</namevalue>
</property>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
PLZ检查这个代码。