如何提取特定元素 [英] how to extract particular elements

查看:32
本文介绍了如何提取特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用姓名"提取客户姓名并将其保存在一个变量中.输入是任何虚拟 xml

像响应变量应该只有这个

 <customer name="Name">John</customer><customer name="Name">Kevin</customer><customer name="Name">Leon</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:变量名=请求"><客户><customer name="Address">1 Doe Place</customer><customer name="State">OH</customer><客户名称="姓名">约翰</客户><customer name="Name">Kevin</customer><customer name="Name">Leon</customer><customer name="Name">Adam</customer><customer name="city">哥伦布</customer></客户></xsl:变量><xsl:变量名=响应"><xsl:copy-of select="$request/customers/customer/@name[.='Name']"/></xsl:变量><xsl:copy-of select="$response"/></xsl:模板></xsl:stylesheet>

但是失败了

解决方案

将您的 xsl:copy-of/@select 更改为:

 

您可能还应该进行一些其他改进:

  • 删除未使用的 xmlns:xs="http://www.w3.org/2001/XMLSchema"命名空间声明.
  • 将您的输出定义为 XML 并通过 xsl:output 缩进以使其看起来不错.
  • 将您的输出包装在一个根元素中,使其成为格式良好的 XML.

总的来说,这个 XSLT:

将产生此输出:

更新以回答 OP 的后续问题

<块引用>

如果我需要像 John;凯文;Leon;Adam

既然您已经用 XSLT 2.0 标记了您的问题,请利用 string-join():

</客户></xsl:模板></xsl:stylesheet>

更新 2 以回答 OP 的第二个后续问题

<块引用>

如果我有多个名字,有些名字是重复的怎么办.我需要过滤重复元素

使用distinct-values():

 <xsl:value-of select="string-join(distinct-values($request/customers/customer[@name='Name']),';')"/>

I need to extract customer names with "Name" and save it in a variable. Input is any dummy xml

like response variable should have only this

        <customer name="Name">John</customer>
        <customer name="Name">Kevin</customer>
        <customer name="Name">Leon</customer>
        <customer name="Name">Adam</customer

used this stylesheet

<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>

but it failed

解决方案

Change your xsl:copy-of/@select to:

  <xsl:copy-of select="$request/customers/customer[@name = 'Name']"/>

You probably should also make a few other improvements:

  • Drop the unused xmlns:xs="http://www.w3.org/2001/XMLSchema" namespace declaration.
  • Define your output as XML and indent it via xsl:output so it looks good.
  • Wrap your output in a root element so that it's well-formed XML.

Altogether, then, this 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>

Will yield this output:

<?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>

Update to answer OP's follow-up question

what if I need like <customer name="Name">John; Kevin; Leon;Adam</customer>

Since you've tagged your question with XSLT 2.0, take advantage of 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>

Update 2 to answer OP's second follow-up question

And what if I have multiple names some are repetitive. I need to filter that duplicate elements

Use distinct-values():

  <xsl:value-of select="string-join(distinct-values($request/customers/customer[@name='Name']),
                                   '; ')"/>

这篇关于如何提取特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆