0

Would like help to trim the result set elements from my overall XML. The schema of the XML sting is not needed. Would only like the resultSet elements of the xmls.

Below is my xml string:

<NewDataSet>
           <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
              <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="resultSet" msdata:UseCurrentLocale="true">
                 <xs:complexType>
                    <xs:choice minOccurs="0" maxOccurs="unbounded">
                       <xs:element name="resultSet">
                          <xs:complexType>
                             <xs:sequence>
                                <xs:element name="Name" type="xs:string" minOccurs="0" />
                                <xs:element name="WAMUserName" type="xs:string" minOccurs="0" />
                                <xs:element name="AppPoolState" type="xs:string" minOccurs="0" />
                                <xs:element name="Validation" type="xs:string" minOccurs="0" />
                             </xs:sequence>
                          </xs:complexType>
                       </xs:element>
                    </xs:choice>
                 </xs:complexType>
              </xs:element>
           </xs:schema>
           <resultSet>
              <Name />
              <WAMUserName />
              <AppPoolState />
              <Validation />
           </resultSet>
           <resultSet>
              <Name>W3SVC/APPPOOLS/KTSAAppPool</Name>
              <WAMUserName>ST\nzsktsa</WAMUserName>
              <AppPoolState>Disabling</AppPoolState>
              <Validation>Critical</Validation>
           </resultSet>
           <resultSet>
              <Name>W3SVC/APPPOOLS/KCPSAAppPool</Name>
              <WAMUserName>st\nzsOFSA</WAMUserName>
              <AppPoolState>Running</AppPoolState>
              <Validation>Critical</Validation>
           </resultSet>
</NewDataSet>

Would like the below output as part of regex expression.

<resultSet>
      <Name />
      <WAMUserName />
      <AppPoolState />
      <Validation />
   </resultSet>
   <resultSet>
      <Name>W3SVC/APPPOOLS/KTSAAppPool</Name>
      <WAMUserName>ST\nzsktsa</WAMUserName>
      <AppPoolState>Disabling</AppPoolState>
      <Validation>Critical</Validation>
   </resultSet>
   <resultSet>
      <Name>W3SVC/APPPOOLS/KCPSAAppPool</Name>
      <WAMUserName>st\nzsOFSA</WAMUserName>
      <AppPoolState>Running</AppPoolState>
      <Validation>Critical</Validation>
</resultSet>
Vivek
  • 43
  • 7

1 Answers1

1

Your code is a mess. But however, you shouldn't use RegEx'es to process XML files. Use an XSLT processor like xsltproc (Linux) or Saxon (Java) or Xerces (Java).

Using an XSLT-1.0 processor, you can extract the resultSet elements easily with the following template.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

  <xsl:template match="/NewDataSet">
    <xsl:apply-templates select="resultSet" />
  </xsl:template>

</xsl:stylesheet>

Apply it to your XML file and the result will be

<?xml version="1.0"?>
<resultSet>
    <Name/>
    <WAMUserName/>
    <AppPoolState/>
    <Validation/>
</resultSet>
<resultSet>
    <Name>W3SVC/APPPOOLS/KTSAAppPool</Name>
    <WAMUserName>ST\nzsktsa</WAMUserName>
    <AppPoolState>Disabling</AppPoolState>
    <Validation>Critical</Validation>
</resultSet>
<resultSet>
    <Name>W3SVC/APPPOOLS/KCPSAAppPool</Name>
    <WAMUserName>st\nzsOFSA</WAMUserName>
    <AppPoolState>Running</AppPoolState>
    <Validation>Critical</Validation>
</resultSet>

The command depends on your XSLT processor.

zx485
  • 28,498
  • 28
  • 50
  • 59