如何提取特定元素

时间:2014-11-26 17:58:35

标签: xml xslt xslt-1.0 xslt-2.0

我需要使用“名称”提取客户名称并将其保存在变量中。输入是任何虚拟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>

但失败了

2 个答案:

答案 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" 名称空间声明。
  • 将输出定义为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']),
                                   '; ')"/>