Using Saxon-HE 10.9 with XSLT 3.0, I am trying to convert unescaped XML string embedded in a larger XML file. I have accomplished this by using
<xsl:template match="INTERNAL_MATCH" mode="dataset">
<xsl:variable name="unescapedXML" select="parse-xml(.)"/>
<TESTVAL>
<xsl:element name="output-params2">
<xsl:copy-of select="//$unescapedXML" />
</xsl:element>
</TESTVAL>
</xsl:template>
and the output reads as such:
<TESTVAL>
<output-params2>
<product ism:ownerProducer="USA">
<name>Test Product</name>
<shortName>TP</shortName>
<base type="testtype" id="1235"/>
<base type="testtype2" id="1236"/>
</product>
</output-params2>
</TESTVAL>
However, I want to access the child nodes of this output instead to be able to parse the base elements by their type, if type = 'testtype' then store it's id as variable.
How would I accomplish this? I tried doing accessor
<xsl:copy-of select="//$unescapedXML/product/base" />
But it returns nothing.
parse the base elements by their type, if type = 'testtype' then store it's id
Given the following input:
XML
<INTERNAL_MATCH><product ownerProducer="USA"><name>Test Product</name><shortName>TP</shortName><base type="testtype" id="1235"/><base type="testtype2" id="1236"/></product></INTERNAL_MATCH>
this stylesheet:
XSLT 3.0
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="INTERNAL_MATCH">
<xsl:variable name="unescapedXML" select="parse-xml(.)"/>
<TESTVAL>
<output-params2>
<xsl:value-of select="$unescapedXML/product/base[@type='testtype']/@id" />
</output-params2>
</TESTVAL>
</xsl:template>
</xsl:stylesheet>
will return:
Result
<?xml version="1.0" encoding="UTF-8"?>
<TESTVAL>
<output-params2>1235</output-params2>
</TESTVAL>
I have removed the undeclared namespace prefix ism:
from your input example. I assume that in your real input this prefix is properly declared and bound to a namespace, otherwise you would get an error instead of the result you report.
In addition, as noted in the comments to your question, it is quite possible that your real input contains another namespace declaration that puts the product
and/or base
elements in a namespace of their own. In such case your stylesheet must take this into account - see here how.