Last active
April 7, 2020 14:31
-
-
Save aykononov/9d904c2a256e09b8f1f4e0dbb91fe18f to your computer and use it in GitHub Desktop.
Выборка из XML с помощью XSLT.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Имеется: | |
xmltype(' | |
<root> | |
<row> | |
<col>v11</col> | |
<col>v12</col> | |
<col>v13</col> | |
<col>v14</col> | |
</row> | |
<row> | |
<col>v21</col> | |
<col>v22</col> | |
<col>v23</col> | |
<col>v24</col> | |
</row> | |
</root>') | |
Необходимо: | |
Получить в виде результата колонку с типом xmltype SQL запроса со следующей структурой: | |
<root> | |
<data row="1" col="1">v11</data> | |
<data row="1" col="2">v12</data> | |
<data row="1" col="3">v13</data> | |
<data row="1" col="4">v14</data> | |
<data row="2" col="1">v21</data> | |
<data row="2" col="2">v22</data> | |
<data row="2" col="3">v23</data> | |
<data row="2" col="4">v24</data> | |
</root> | |
Реализовать данный запрос используя XSLT трансформацию. | |
Условие: количество узлов row и col может варьироваться | |
*/ | |
SELECT XMLTRANSFORM( | |
XMLTYPE('<root> | |
<row> | |
<col>v11</col> | |
<col>v12</col> | |
<col>v13</col> | |
<col>v14</col> | |
</row> | |
<row> | |
<col>v21</col> | |
<col>v22</col> | |
<col>v23</col> | |
<col>v24</col> | |
</row> | |
</root>'), | |
XMLTYPE('<xsl:stylesheet version="1.0" | |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | |
<xsl:template match="/root"> | |
<xsl:element name="root"> | |
<xsl:for-each select="row"> | |
<xsl:variable name="row" select="position()"/> | |
<xsl:for-each select="col"> | |
<data row="{$row}" col="{position()}"> | |
<xsl:value-of select="."/> | |
</data> | |
</xsl:for-each> | |
</xsl:for-each> | |
</xsl:element> | |
</xsl:template> | |
</xsl:stylesheet>') | |
) AS xml_xslt | |
FROM dual; |
Author
aykononov
commented
Nov 11, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment