簡體   English   中英

從XSL源訪問內部XML數據

[英]Accessing the inner XML data from XSL source

我正在嘗試訪問XSL文檔中的內部XML數據。 嘗試這樣做時,Apache Xalan在使用document('')時引發java.lang.NullPointerException。

這是XSL來源:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:fo="http://www.w3.org/1999/XSL/Format"
 xmlns:ext="http://exslt.org/common"
 xmlns:my="http://example.com/2006/some-data">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <my:params xml:space="preserve">
        <pattern>
            <old>&lt;p&gt;</old>
            <new>P</new>
        </pattern>
        <pattern>
            <old>&lt;/p&gt;</old>
            <new>/P</new>
        </pattern>
        <pattern>
            <old>&lt;strong&gt;</old>
            <new>STRONG</new>
        </pattern>
        <pattern>
            <old>&lt;/strong&gt;</old>
            <new>/STRONG</new>
        </pattern>
    </my:params>

    <xsl:variable name="vrtfPats">
     <xsl:for-each select="document('')/xsl:stylesheet/my:params/*">
       <xsl:copy-of select="."/>
     </xsl:for-each>
    </xsl:variable>


</xsl:stylesheet>

還有其他方法可以使用Xalan從XSL文件訪問內部數據嗎?

盡量不要聲明一個變量來存儲節點並在樣式表中使用它們,如下所示:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:ext="http://exslt.org/common"
xmlns:my="http://example.com/2006/some-data">
<xsl:output omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>

<my:params xml:space="preserve">
    <pattern>
        <old>&lt;p&gt;</old>
        <new>P</new>
    </pattern>
    <pattern>
        <old>&lt;/p&gt;</old>
        <new>/P</new>
    </pattern>
    <pattern>
        <old>&lt;strong&gt;</old>
        <new>STRONG</new>
    </pattern>
    <pattern>
        <old>&lt;/strong&gt;</old>
        <new>/STRONG</new>
    </pattern>
</my:params>

<xsl:template match="/">
    <test>
    <xsl:for-each select="document('')//my:params">
        <xsl:apply-templates/>
    </xsl:for-each>
    </test>
</xsl:template>
<xsl:template match="pattern">
    <pattern-match>
        <xsl:apply-templates/>
    </pattern-match>
</xsl:template>
<xsl:template match="old">
    <old-match>
        <xsl:apply-templates/>
    </old-match>
</xsl:template>
<xsl:template match="new">
    <new-match>
        <xsl:apply-templates/>
    </new-match>
</xsl:template>

</xsl:stylesheet>

產生此答案:

    <test xmlns:my="http://example.com/2006/some-data" xmlns:ext="http://exslt.org/common"
        xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <pattern-match>
            <old-match>&lt;p&gt;</old-match>
            <new-match>P</new-match>
        </pattern-match>
        <pattern-match>
            <old-match>&lt;/p&gt;</old-match>
            <new-match>/P</new-match>
        </pattern-match>
        <pattern-match>
            <old-match>&lt;strong&gt;</old-match>
            <new-match>STRONG</new-match>
        </pattern-match>
        <pattern-match>
            <old-match>&lt;/strong&gt;</old-match>
            <new-match>/STRONG</new-match>
        </pattern-match>
    </test>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM