我需要使用“名称”提取客户名称并将其保存在变量中。输入是任何虚拟xml
像响应变量应该只有这个
<customer name="Name">John</customer>
<customer name="Name">Kevin</customer>
<customer name="Name">Leon</customer>
<customer name="Name">Adam</customer
使用了这个样式表
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/">
<xsl:variable name="request">
<customers>
<customer name="Address">1 Doe Place</customer>
<customer name="State">OH</customer>
<customer name="Name">John</customer>
<customer name="Name">Kevin</customer>
<customer name="Name">Leon</customer>
<customer name="Name">Adam</customer>
<customer name="city">Columbus</customer>
</customers>
</xsl:variable>
<xsl:variable name="response">
<xsl:copy-of select="$request/customers/customer/@name[. = 'Name']"/>
</xsl:variable>
<xsl:copy-of select="$response"/>
</xsl:template>
</xsl:stylesheet>
但失败了
答案 0 :(得分:2)
我认为您需要customer
元素的谓词,如
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/">
<xsl:variable name="request">
<customers>
<customer name="Address">1 Doe Place</customer>
<customer name="State">OH</customer>
<customer name="Name">John</customer>
<customer name="Name">Kevin</customer>
<customer name="Name">Leon</customer>
<customer name="Name">Adam</customer>
<customer name="city">Columbus</customer>
</customers>
</xsl:variable>
<xsl:variable name="response" select="$request/customers/customer[@name = 'Name']"/>
<xsl:copy-of select="$response"/>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:1)
将您的xsl:copy-of/@select
更改为:
<xsl:copy-of select="$request/customers/customer[@name = 'Name']"/>
您可能还应该做一些其他改进:
xmlns:xs="http://www.w3.org/2001/XMLSchema"
名称空间声明。xsl:output
缩进,以使其看起来不错。然后,这个XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="request">
<customers>
<customer name="Address">1 Doe Place</customer>
<customer name="State">OH</customer>
<customer name="Name">John</customer>
<customer name="Name">Kevin</customer>
<customer name="Name">Leon</customer>
<customer name="Name">Adam</customer>
<customer name="city">Columbus</customer>
</customers>
</xsl:variable>
<xsl:variable name="response">
<xsl:copy-of select="$request/customers/customer[@name = 'Name']"/>
</xsl:variable>
<customers>
<xsl:copy-of select="$response"/>
</customers>
</xsl:template>
</xsl:stylesheet>
将产生此输出:
<?xml version="1.0" encoding="UTF-8"?>
<customers>
<customer name="Name">John</customer>
<customer name="Name">Kevin</customer>
<customer name="Name">Leon</customer>
<customer name="Name">Adam</customer>
</customers>
如果我需要
,该怎么办?<customer name="Name">John; Kevin; Leon;Adam</customer>
由于您已使用XSLT 2.0标记了您的问题,因此请利用string-join()
:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="request">
<customers>
<customer name="Address">1 Doe Place</customer>
<customer name="State">OH</customer>
<customer name="Name">John</customer>
<customer name="Name">Kevin</customer>
<customer name="Name">Leon</customer>
<customer name="Name">Adam</customer>
<customer name="city">Columbus</customer>
</customers>
</xsl:variable>
<customer name="Name">
<xsl:value-of select="string-join($request/customers/customer[@name='Name'],
'; ')"/>
</customer>
</xsl:template>
</xsl:stylesheet>
如果我有多个名字,有些是重复的。我需要 过滤重复元素
使用distinct-values()
:
<xsl:value-of select="string-join(distinct-values($request/customers/customer[@name='Name']),
'; ')"/>