Sdílet prostřednictvím


Použití transformace XSLT na datovou sadu

Metoda WriteXml objektu DataSet umožňuje zapsat obsah DataSet jako XML data. Běžným úkolem je pak transformovat xml do jiného formátu pomocí transformací XSL (XSLT). Synchronizace DataSet s XmlDataDocument však umožňuje použít šablonu stylů XSLT na obsah DataSet, aniž byste museli nejprve zapsat DataSet jako XML data pomocí WriteXml.

Následující příklad naplní DataSet tabulky a relacemi, synchronizuje DataSet s XmlDataDocument a zapíše část DataSet souboru ve formátu HTML pomocí šablony stylů XSLT. Obsah šablony stylů XSLT:

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

<xsl:template match="CustomerOrders">
  <HTML>
  <STYLE>
  BODY {font-family:verdana;font-size:9pt}
  TD   {font-size:8pt}
  </STYLE>
    <BODY>
    <TABLE BORDER="1">
      <xsl:apply-templates select="Customers"/>
    </TABLE>
    </BODY>
  </HTML>
</xsl:template>

<xsl:template match="Customers">
    <TR><TD>
      <xsl:value-of select="ContactName"/>, <xsl:value-of select="Phone"/><BR/>
    </TD></TR>
      <xsl:apply-templates select="Orders"/>
</xsl:template>

<xsl:template match="Orders">
  <TABLE BORDER="1">
    <TR><TD valign="top"><B>Order:</B></TD><TD valign="top"><xsl:value-of select="OrderID"/></TD></TR>
    <TR><TD valign="top"><B>Date:</B></TD><TD valign="top"><xsl:value-of select="OrderDate"/></TD></TR>
    <TR><TD valign="top"><B>Ship To:</B></TD>
        <TD valign="top"><xsl:value-of select="ShipName"/><BR/>
        <xsl:value-of select="ShipAddress"/><BR/>
        <xsl:value-of select="ShipCity"/>, <xsl:value-of select="ShipRegion"/>  <xsl:value-of select="ShipPostalCode"/><BR/>
        <xsl:value-of select="ShipCountry"/></TD></TR>
  </TABLE>
</xsl:template>

</xsl:stylesheet>

Následující kód vyplní DataSet a použije šablonu stylů XSLT.

Poznámka:

Pokud používáte šablonu stylů XSLT na DataSet obsahující relace, dosáhnete nejlepší výkon, pokud nastavíte vlastnost DataRelation na hodnotu true pro každou vnořenou relaci. To umožňuje používat XSLT šablony stylů, které implementují přirozené zpracování shora dolů pro navigaci hierarchií a transformaci dat, na rozdíl od používání os lokací XPath, které jsou náročné na výkon (například preceding-sibling a following-sibling ve výrazech testů uzlů šablon stylů) při navigaci. Další informace o vnořených relacích najdete v tématu Vnoření datových relací.

' Assumes connection is a valid SqlConnection.
Dim dataSet As DataSet = New DataSet("CustomerOrders")

Dim customerAdapter As SqlDataAdapter = New SqlDataAdapter( _
  "SELECT * FROM Customers", connection)
customerAdapter.Fill(dataSet, "Customers")

Dim orderAdapter As SqlDataAdapter = New SqlDataAdapter( _
  "SELECT * FROM Orders", connection)
orderAdapter.Fill(dataSet, "Orders")

connection.Close()

dataSet.Relations.Add("CustOrders", _
dataSet.Tables("Customers").Columns("CustomerID"), _
dataSet.Tables("Orders").Columns("CustomerID")).Nested = true

Dim xmlDoc As XmlDataDocument = New XmlDataDocument(dataSet)

Dim xslTran As XslTransform = New XslTransform
xslTran.Load("transform.xsl")

Dim writer As XmlTextWriter = New XmlTextWriter( _
  "xslt_output.html", System.Text.Encoding.UTF8)

xslTran.Transform(xmlDoc, Nothing, writer)
writer.Close()
// Assumes connection is a valid SqlConnection.
connection.Open();

DataSet custDS = new DataSet("CustomerDataSet");

SqlDataAdapter customerAdapter = new SqlDataAdapter(
  "SELECT * FROM Customers", connection);
customerAdapter.Fill(custDS, "Customers");

SqlDataAdapter orderAdapter = new SqlDataAdapter(
  "SELECT * FROM Orders", connection);
orderAdapter.Fill(custDS, "Orders");

connection.Close();

custDS.Relations.Add("CustOrders",
  custDS.Tables["Customers"].Columns["CustomerID"],
                     custDS.Tables["Orders"].Columns["CustomerID"]).Nested = true;

XmlDataDocument xmlDoc = new XmlDataDocument(custDS);

XslTransform xslTran = new XslTransform();
xslTran.Load("transform.xsl");

XmlTextWriter writer = new XmlTextWriter("xslt_output.html",
  System.Text.Encoding.UTF8);

xslTran.Transform(xmlDoc, null, writer);
writer.Close();

Viz také