我需要用"Name"提取客户名称并将其保存在变量中。输入是任何虚拟的 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>
但它失败了
我认为您需要在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>
将xsl:copy-of/@select
更改为:
<xsl:copy-of select="$request/customers/customer[@name = 'Name']"/>
您可能还应该进行一些其他改进:
- 删除未使用的
xmlns:xs="http://www.w3.org/2001/XMLSchema"
命名空间声明。 - 将输出定义为 XML 并通过
xsl:output
缩进它,使其看起来不错。 - 将输出包装在根元素中,使其是格式正确的 XML。
那么,总的来说,这个 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>
更新以回答OP的后续问题
如果我需要像
<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>
更新 2 以回答 OP 的第二个后续问题
如果我有多个名字,有些是重复的怎么办。我需要 过滤重复元素
使用distinct-values()
:
<xsl:value-of select="string-join(distinct-values($request/customers/customer[@name='Name']),
'; ')"/>