简体   繁体   中英

Shred XML and show in SSRS Report

I have XML like so

<OrchestrationConfiguration>
<Instrument>
<Spectrometer>
    <Inputs>
      <Channel-ChannelNumber-1>
        <ChannelNumber>1</ChannelNumber>
        <ChannelName>Answer01</ChannelName>
      </Channel-ChannelNumber-1>
    </Inputs>
    <Outputs>
      <Channel-ChannelNumber-1>
        <ChannelNumber>1</ChannelNumber>
        <ChannelName>1</ChannelName>
        <ChannelName>2</ChannelName>
        <ChannelName>3</ChannelName>
        <ChannelName>4</ChannelName>
        <ChannelName>5</ChannelName>
        <ChannelName>6</ChannelName>
        <ChannelName>7</ChannelName>
        <ChannelName>8</ChannelName>
        <ChannelName>9</ChannelName>
        <ChannelName>10</ChannelName>
      </Channel-ChannelNumber-1>
      <Channel-ChannelNumber-2>
      <ChannelNumber>2</ChannelNumber>
      <ChannelName>Temperature-K</ChannelName>
      </Channel-ChannelNumber-2>
      <Channel-ChannelNumber-3>
        <ChannelNumber>3</ChannelNumber>
        <ChannelName>Pressure-inHg-absolute</ChannelName>
        </Channel-ChannelNumber-3>
    </Outputs>
</Spectrometer>
</OrchestrationConfiguration>

In my SSRS report. I have created a list grouped by Parent Name and to get the outputs I have applied the following filters 输出滤波器

The issue I am having is to get the Inputs. I am unable to distinguish between Outputs and Inputs as the filter is the same. Is there any other way of doing this? I am happy to change the format of XML if required. 输入过滤器

I am using the XMLTable function to shred the xml. Shred XML

Notes which might help

  1. I am using SQL Server 2014.
  2. The above XML is present in a database table in SQL.

In your comment you state, that the XML can be changed. You really should do this.

Some background:

  • The order of nodes in XML is persisted. There is no need to state the "position" as long as it is the same as the actual position within the node order. Still it can be a good idea to state a ChannelNumber literally, as it can simplify the queries.
  • It is easy to ask an XML *Show me all <Channel> elements below <Outputs> , but it needs hacky workarounds to ask Show me all elements below <Outputs> , which have a name starting with Channel .

Try it like this:

DECLARE @XML XML=
'<OrchestrationConfiguration>
  <Instrument>
    <Spectrometer>
      <Inputs>
        <Channel Number="1">
          <ChannelName>Answer01</ChannelName>
        </Channel>
      </Inputs>
      <Outputs>
        <Channel Number="1">
          <ChannelName>1</ChannelName>
          <ChannelName>2</ChannelName>
          <ChannelName>3</ChannelName>
          <ChannelName>4</ChannelName>
          <ChannelName>5</ChannelName>
          <ChannelName>6</ChannelName>
          <ChannelName>7</ChannelName>
          <ChannelName>8</ChannelName>
          <ChannelName>9</ChannelName>
          <ChannelName>10</ChannelName>
        </Channel>
        <Channel Number="2">
          <ChannelName>Temperature-K</ChannelName>
        </Channel>
        <Channel Number="3">
          <ChannelName>Pressure-inHg-absolute</ChannelName>
        </Channel>
      </Outputs>
    </Spectrometer>
  </Instrument>
</OrchestrationConfiguration>';

--I do not know, how you need the data, but with SQL-Server I'd implement an inline table valued function , passing in the XML and retrieving the data as derived table. Use something like this:

SELECT InpCh.value('@Number','int') AS InputChannelNumber
      ,InpCh.value('(ChannelName/text())[1]','nvarchar(max)') AS InputChannelName
      ,OutCh.value('@Number','int') AS InputChannelNumber
      ,OutChName.value('(text())[1]','nvarchar(max)') AS InputChannelName
FROM @xml.nodes('/OrchestrationConfiguration/Instrument/Spectrometer/Inputs/Channel') AS A(InpCh)
OUTER APPLY @xml.nodes('/OrchestrationConfiguration/Instrument/Spectrometer/Outputs/Channel') AS B(OutCh)
OUTER APPLY B.OutCh.nodes('ChannelName') AS C(OutChName)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM