XSLT - How to treat inline/escaped XML within a node as nested nodes

只愿长相守 提交于 2021-01-29 13:45:11

问题


New to xslt - not enjoying it so far.

Trying to preserve some HTML (table) formatting being received by an application that converts to PDF.

Need an xslt way of interpreting escaped HTML/XML data within an XML node as further child nodes?

Have tried a v3 parse-xml() on that node.
Have tried a v1 search/replace template to convert &lt; into < etc. Have tried disable-output-escaping="yes" - nope.

Nothing appears to work for some reason.

Source XML:

<?xml version="1.0" encoding="windows-1252"?>
<Report>
    <node1>node1</node1>
    <node2>node2</node2>
    <node3>node3</node3>
    <node4>
        <node4a_with_nested_xml>Nestedxml text$lt;br/$gt;
            $lt;b$gt;
                $lt;u$gt;blah blah blah$lt;/u$gt;
            $lt;/b$gt;
            $lt;br/$gt;
            $lt;table$gt;
                $lt;tr$gt;
                    $lt;td$gt;
                        $lt;br/$gt;blah blah blah$lt;br/$gt;
                    $lt;/td$gt;
                $lt;/tr$gt;
            $lt;/table$gt;
            $lt;b$gt;
                $lt;u$gt;blah blah blah$lt;/u$gt;
            $lt;/b$gt;
            $lt;br/$gt;
            $lt;table$gt;
                $lt;tr$gt;
                    $lt;td$gt;
                        $lt;br/$gt;blah blah blah$lt;/td$gt;
                    $lt;td$gt;blah blah blah$lt;br/$gt;Other:$lt;/td$gt;
                    $lt;td$gt;blah blah blah$lt;br/$gt;
                    $lt;/td$gt;
                $lt;/tr$gt;
            $lt;/table$gt;
        </node4a_with_nested_xml>
    </node4>
</Report>

Needs to be interpreted as:

<?xml version="1.0" encoding="windows-1252"?>
<Report>
    <node1>node1</node1>
    <node2>node2</node2>
    <node3>node3</node3>
    <node4>
        <node4a_with_nested_xml>
            Nestedxml text
            <br/>
                <b>
                    <u>blah blah blah</u>
                </b>
            <br/>
            <table>
                <tr>
                    <td>
                        <br/>blah blah blah<br/>
                    </td>
                </tr>
            </table>
            <b>
                <u>blah blah blah</u>
            </b>
            <br/>
            <table>
                <tr>
                    <td>
                        <br/>blah blah blah
                    </td>
                    <td>
                    blah blah blah<br/>Other:
                    </td>
                    <td>blah blah blah<br/>
                    </td>
                </tr>
            </table>
        </node4a_with_nested_xml>
    </node4>
</Report>

Then from there i can start pulling in the child nodes & do stuff with them eg:

<xsl:for-each select="Report">
    <xsl:for-each select="node4">
        <xsl:for-each select="node4a_with_nested_xml">
            <xsl:value-of select="."/>
            <xsl:for-each select="table">
                <fo:table>
                    <xsl:for-each select="tr">
                    <fo:table-row>
                        <xsl:for-each select="td">
                        <fo:table-cell>
                            <xsl:value-of select="."/>
                            <xsl:for-each select="br">
                                <fo:block/>
                            </xsl:for-each>
                        </fo:table-cell>
                        </xsl:for-each>
                    </fo:table-row>
                </xsl:for-each>
                </fo:table>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:for-each>
</xsl:for-each>

回答1:


In XSLT 1.0 you must do the transformation in two passes: first, disable output escaping on node4a_with_nested_xml and save the result to a file; then process the resulting file using another XSLT stylesheet.

This is assuming that your processor supports disable-output-escaping and that the escaped string contains well-formed XML.



来源:https://stackoverflow.com/questions/57798465/xslt-how-to-treat-inline-escaped-xml-within-a-node-as-nested-nodes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!