Extracting prefixed attributes using XSLT

For the below XML

<SampleXMLFormat>
  <Header>
    <Id>123</Id>
  </Header>
  <Properties>
    <property name="a1_prop1" value="val1"/>
    <property name="a1_prop2" value="val2"/>
    <property name="a2_prop1" value="val1"/>
    <property name="a2_prop2" value="val2"/>
    <property name="abcd" value="efgh"/>
  </Properties>
</SampleXMLFormat>

Can I please get the XSLT transformations for getting the following outputs:

1) Without properties with prefix a2

<SampleXMLFormat>
  <Header>
    <Id>123</Id>
  </Header>
  <Properties>
    <property name="a1_prop1" value="val1"/>
    <property name="a1_prop2" value="val2"/>
    <property name="abcd" value="efgh"/>
  </Properties>
</SampleXMLFormat>

2) Only properties with prefix a2

<property name="a2_prop1" value="val1"/>
<property name="a2_prop2" value="val2"/>

@ABach For the 2nd part, im trying this..Instead of selecting all nodes,i am trying to select only property nodes ,but i get malformed output.Can you please advise what im doing wrong?

xslt = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="property|node()">
    <xsl:copy>
      <xsl:apply-templates select="property|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="property[not(starts-with(@name, \'BOND_\'))]"/>
</xsl:stylesheet>';

Answers


ANSWER #1

When this XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="property[starts-with(@name, 'a2')]"/>

</xsl:stylesheet>

...is applied against the provided XML:

<SampleXMLFormat>
  <Header>
    <Id>123</Id>
  </Header>
  <Properties>
    <property name="a1_prop1" value="val1"/>
    <property name="a1_prop2" value="val2"/>
    <property name="a2_prop1" value="val1"/>
    <property name="a2_prop2" value="val2"/>
    <property name="abcd" value="efgh"/>
  </Properties>
</SampleXMLFormat>

...the wanted result is produced:

<SampleXMLFormat>
  <Header>
    <Id>123</Id>
  </Header>
  <Properties>
    <property name="a1_prop1" value="val1"/>
    <property name="a1_prop2" value="val2"/>
    <property name="abcd" value="efgh"/>
  </Properties>
</SampleXMLFormat>

ANSWER #2

When this XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="property[not(starts-with(@name, 'a2'))]"/>

</xsl:stylesheet>

...is applied against the provided XML:

<SampleXMLFormat>
  <Header>
    <Id>123</Id>
  </Header>
  <Properties>
    <property name="a1_prop1" value="val1"/>
    <property name="a1_prop2" value="val2"/>
    <property name="a2_prop1" value="val1"/>
    <property name="a2_prop2" value="val2"/>
    <property name="abcd" value="efgh"/>
  </Properties>
</SampleXMLFormat>

...the wanted result is produced:

<SampleXMLFormat>
  <Header>
    <Id>123</Id>
  </Header>
  <Properties>
    <property name="a2_prop1" value="val1"/>
    <property name="a2_prop2" value="val2"/>
  </Properties>
</SampleXMLFormat>

EDIT: the OP has added some clarity on the second question. In that case, this XSLT should do the trick:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/*">
    <xsl:copy-of select="*/property[starts-with(@name, 'a2')]" />
  </xsl:template>

</xsl:stylesheet>

Need Your Help

How I can use a css and javascript menu in my asp.net application?

c# javascript html asp.net css

hi i want test a menu in my asp.net application but i get a javascript error.

jQuery Drag/Drop/Sortable-Example: Sorting during moving (= dragging process )

jquery html jquery-ui drag-and-drop jquery-ui-sortable

The following source code is a working example. I can drag and drop and sort afterwords. But I do not only want to sort afterwords, also during the dragging process. What exactly should I add here?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.