how to extract particular elements

2019-09-01 06:33发布

问题:

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

回答1:

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']),
                                   '; ')"/>


回答2:

I think you need a predicate on the customer element, as in

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