我已經在網上搜索了一個工具來做到這一點,但我還沒有找到一個,所以我認爲創建自己應該很簡單。 我想創建一個XSLT,我輸入一個任意的xml文件,它會輸出一個select語句,我可以在Oracle數據庫中使用它來生成輸入xml。使用XSLT創建一個PLSQL語句來輸出XML
E.g. 如果我給它:
<?xml version="1.0"?>
<test xmlns="dddd" xmlns:xxx="ddd222" someatt="val">
<xxx:f>E</xxx:f>
<g>G</g>
<h xmlns="anotherns">H</h>
<zz:i xmlns:zz="yetanotherns">I</zz:i>
</test>
我想下面的輸出:
select
xmlelement("test"
,xmlattributes(
'dddd' as "xmlns"
,'ddd222' as "xmlns:xxx"
,'val' as "someatt"
)
,xmlelement("xxx:f",'E')
,xmlelement("g",'G')
,xmlelement("h"
,xmlattributes('anotherns' as "xmlns")
,'H'
)
,xmlelement("zz:i"
,xmlattributes('yetanotherns' as "xmlns:zz")
,'I'
)
)
from dual;
我在那裏幾乎所有的方式。我可以使用當前的XSLT進行以下輸出:
select
xmlelement("test"
,xmlattributes(
'val' as "someatt"
)
,xmlelement("xxx:f",'E')
,xmlelement("g",'G')
,xmlelement("h",'H')
,xmlelement("zz:i",'I')
)
from dual;
除缺少xmlns屬性外,這是完美的。問題是輸入文檔中的xmlns和xmlns:***屬性不被視爲普通屬性,並且在運行xslt時似乎不可見。 有沒有讓他們保持的選擇?
的XSLT我有低於:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
>
<xsl:output method="text" indent="no" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:text>select
</xsl:text>
<xsl:apply-templates/>
<xsl:text>
from dual;</xsl:text>
</xsl:template>
<xsl:template match="node()">
<xsl:text>xmlelement("</xsl:text>
<xsl:value-of select='name()'/>
<xsl:text>"</xsl:text>
<!--Lots of tabs for indenting-->
<xsl:variable name='tabs' xml:space="preserve"> </xsl:variable>
<xsl:variable name='nl'><xsl:text>
</xsl:text><xsl:value-of select='substring($tabs,0,count(ancestor::*)+2)'/></xsl:variable>
<xsl:variable name='att_children' select='count(@*)'/>
<xsl:if test="$att_children > 0">
<xsl:value-of select="$nl"/>
<xsl:text>,xmlattributes(</xsl:text>
<xsl:for-each select='./@*'>
<xsl:value-of select="$nl"/><xsl:text> </xsl:text>
<xsl:if test="position() > 1"><xsl:text>,</xsl:text></xsl:if>
<xsl:text>'</xsl:text>
<xsl:value-of select="."/>
<xsl:text>' as "</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>"</xsl:text>
</xsl:for-each>
<xsl:value-of select="$nl"/>
<xsl:text>)</xsl:text>
</xsl:if>
<xsl:variable name='children' select='count(*)'/>
<!--<xsl:value-of select='$children'/>-->
<xsl:choose>
<xsl:when test='$children=0'>
<xsl:text>,</xsl:text>
<xsl:text>'</xsl:text>
<xsl:value-of select='text()'/>
<xsl:text>'</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select='./*'>
<xsl:value-of select="$nl"/>
<xsl:text>,</xsl:text>
<xsl:apply-templates select='.'/>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
<xsl:if test='$children > 1'>
<xsl:value-of select="$nl"/>
</xsl:if>
<xsl:text>)</xsl:text>
</xsl:template>
<xsl:template match="text()|@*"> </xsl:template>
</xsl:stylesheet>
'xmlns'不是一個屬性,它是一個名稱空間聲明。您仍然可以在XSLT中訪問它們,但是您可能需要了解如何在PLSQL/Oracle中處理XML名稱空間,以便您知道首先要生成的輸出,因爲它可能與您當前顯示的預期輸出不同。 –
@TimC似乎(奇怪)它是定義屬性或名稱空間的函數,請參閱http://stackoverflow.com/questions/437670/oracle-how-to-create-an-element-in-a-specific-namespace-with-xmlelement – potame
嗨,是的,我知道什麼輸出我想從xslt,它是在問題中。 PLSQL將xmlns視爲正常的屬性,並且我已經在這個問題中測試了這個語句並且它可以工作。 – Robert3452