开发者

How to match and process unknown XML elements in XSLT 1.0?

开发者 https://www.devze.com 2023-03-04 02:45 出处:网络
I have a simply XSLT 1.0 stylesheet, that turns XML documents in XHTML. I really want to be able to \"include\" the content of an XML file in another when needed. AFAIK it is simply not possible in XS

I have a simply XSLT 1.0 stylesheet, that turns XML documents in XHTML. I really want to be able to "include" the content of an XML file in another when needed. AFAIK it is simply not possible in XSLT 1.0, so I decided to move my processing to a simple Java app开发者_高级运维 that would pre-process the XML, executing the "includes" recursively, and passing it to the default JDK XSLT processor. I have a XML schema that my documents must conform to.

The most used element is called "text", and can have an "id" and/or a "class" attribute, which gets used for XHTML styling with CSS. This element gets turned into "p", "div", or "span" depending on the context.

What I would like to add, is the ability to define "unknown" elements in my input files, and have them transformed in a "text" element for further processing. If the "unknown" element's name start with a capital letter, then it becomes a "text", with "id" set to original name. Otherwise a "text" with "class" set to original name. Everything else in the unknown element should be kept as-is, and then it should be processed by XSLT as if it was originally in the input file. In other words, I would like to transform all unknown elements to for a valid XML document, and then process it with my stylesheet.

Can this be done in XSLT, possibly in a pre-processing "stylesheet", or should I do that as pre-processing in Java? Performance here is not important. I would prefer a XSLT solution, but not if it's much more complicated then doing it in Java.


Well, since no one answered, I just tried it. While is is easier to do it in Java, it has one major drawback: since the code need to know the valid elements so that it recognize the unknown ones, you end up having to hardcode that in your code and have to recompile it if the XSLT template changes.

So, I tried in XSLT and it also works. Let's say you have:

<xsl:template match="text">
    *processing*
    <xsl:call-template name="id_and_class"/>
    *processing*
</xsl:template>

where the template named id_and_class copies your id and classes attribute in the generated element, and you want unknown elements to be mapped to "text" elements, then you can do this:

<xsl:template match="text">
    <xsl:call-template name="text_processing"/>
</xsl:template>

<xsl:template name="text_processing">
    *processing*
    <xsl:call-template name="text_id_and_class"/>
    *processing*
</xsl:template>
...
<xsl:template name="text_id_and_class">
     <xsl:choose>
        <!-- If name() is not "text", then we have an unknown element. -->
        <xsl:when test="name()!='text'">
            <!-- Processing of ID and class omitted ... -->
        </xsl:when>
        <xsl:otherwise>
            <xsl:call-template name="id_and_class"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
...
<!-- MUST BE LAST : Process unknown elements like a "text" element. -->
<xsl:template match="*">
   <xsl:call-template name="text_processing"/>
</xsl:template>

If yon process the content of one specific element with a named template, then you can check in that template if the name matches, and use that for your special processing. Then you just have to put a <xsl:template match="*"> at the end of your stylesheet and call the named template from there.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号