2

Good evening,

I have a problem with passing the result of "parse-xml-fragment" to templates using XSL-T 3.0. I am using Saxon-PE 9.7.0.15 (Java version bundled in the latest oXygen 19). I would like to be able to apply (already) defined templates to the resulted "document-node" from "parse-xml-fragment" as if it was the part of original XML document and not the base64 content of one defined attribute. In other words I need to walk through the unknown content of the result of "parse-xml-fragment".

I am able to get the values from base64 content using for example

<xsl:value-of select="parse-xml-fragment(saxon:base64Binary-to-string(./@a, 'UTF-8'))/base64Root/a/@at"/>

but it supposes, that I know the structure and content of the base64 value of the given attribute, I am do not know.

XML file is:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="a.xsl"?>
<root a="PGJhc2U2NFJvb3Q+PGEgYXQ9IjEiPjwvYT48YiBhdD0iMiI+PC9iPjwvYmFzZTY0Um9vdD4="/>
<!-- 
  content of @a is (and may vary in structure and content): 
  <base64Root><a at="1"></a><b at="2"></b></base64Root>
-->

XSL-T 3.0 file (which does not work as I want) is:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:saxon="http://saxon.sf.net/" exclude-result-prefixes="xs" version="3.0">

  <xsl:output method="text" />

  <xsl:template match="/root">
    <xsl:call-template name="anyElement">
      <xsl:with-param name="xmlFragment" select="parse-xml-fragment(saxon:base64Binary-to-string(./@a, 'UTF-8'))"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="anyElement" match="*">
    <xsl:param name="xmlFragment"/>
    <xsl:value-of select="local-name(.)"/>
    <xsl:value-of select="./@at"/>
    <xsl:apply-templates>
      <xsl:with-param name="xmlFragment" select="."/>
    </xsl:apply-templates>
  </xsl:template>

</xsl:stylesheet>

Desired output is something like (formatting is not important now):

root 
PGJhc2U2NFJvb3Q+PGEgYXQ9IjEiPjwvYT48YiBhdD0iMiI+PC9iPjwvYmFzZTY0Um9vdD4=
base64Root
a
1
b
2

Thank You very much for Your help in advance. Stepan

4

1 回答 1

4

I would simply push the nodes with apply-templates to your template matching element nodes, I am not sure why you call-templates first:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:saxon="http://saxon.sf.net/"
    exclude-result-prefixes="xs math saxon"
    version="3.0">

    <xsl:output method="text" />

    <xsl:template match="/root">
        <xsl:next-match/>
        <xsl:apply-templates select="parse-xml-fragment(saxon:base64Binary-to-string(@a, 'UTF-8'))/node()"/>
    </xsl:template>

    <xsl:template match="*">
        <xsl:value-of select="local-name(), @*" separator="&#10;"/>
        <xsl:text>&#10;</xsl:text>
        <xsl:apply-templates/>   
    </xsl:template>

</xsl:stylesheet>

Output is

root
PGJhc2U2NFJvb3Q+PGEgYXQ9IjEiPjwvYT48YiBhdD0iMiI+PC9iPjwvYmFzZTY0Um9vdD4=
base64Root
a
1
b
2
于 2017-08-02T18:08:42.863 回答