Copy all elements of a namespace and nothing else

2019-02-28 11:03发布

We have a bunch of files that are html pages but which contain additional xml elements (all prefixed with our company name 'TLA') to provide data and structure for an older program which I am now rewriting.

Example Form:

<html >
<head>
    <title>Highly Simplified Example Form</title>
</head>
<body>
    <TLA:document xmlns:TLA="http://www.tla.com">
        <TLA:contexts>
            <TLA:context id="id_1" value=""></TLA:context>
        </TLA:contexts>
        <TLA:page>
            <TLA:question id="q_id_1">
                <table>
                    <tr>
                        <td>
                            <input id="input_id_1" type="text" />
                        </td>
                    </tr>
                </table>
            </TLA:question>
        </TLA:page>
        <!-- Repeat many times -->
    </TLA:document>
</body>
</html>

My task is to write a pre-processor that will extract all the 'TLA' elements and ignore the html elements

Desired XML Output:

<?xml version="1.0" encoding="utf-8" ?>
<TLA:document xmlns:TLA="http://www.tla.com">
    <TLA:contexts>
      <TLA:context id="id_1" value=""></TLA:context>
    </TLA:contexts>
    <TLA:page>
      <TLA:question  id="q_id_1">
      </TLA:question>
    </TLA:page>
    <!-- Repeat many times -->
</TLA:document>

This should be doable with XSLT but I'm unable to formulate the correct code. This is what I have so far:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    xmlns:tla="http://www.tla.com"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="tla:*">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Which is extracting the elements I want (but not their attributes!) but also extracts the text attributes and content of the html elements. How can I exclude the html elements and their content?

2条回答
我命由我不由天
2楼-- · 2019-02-28 11:51

You could try something like this...

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:tla="http://www.tla.com" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:template>

    <xsl:template match="tla:*">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
查看更多
别忘想泡老子
3楼-- · 2019-02-28 11:54

This should do it:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:tla="http://www.tla.com">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
  <xsl:strip-space elements="*" />

  <xsl:template match="text()" />

  <xsl:template match="tla:* | tla:*/@* | tla:*/text()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

When run on your sample input (once the missing namespace declaration is added), the result is:

<TLA:document xmlns:TLA="http://www.tla.com">
  <TLA:contexts>
    <TLA:context id="id_1" value="" />
  </TLA:contexts>
  <TLA:page>
    <TLA:question id="q_id_1" />
  </TLA:page>
</TLA:document>
查看更多
登录 后发表回答