0

I'm having difficulty getting an XSL transform to run successfully in .NET code, even though it runs fine when I put it through the XSLT debugger in VS2012. I'm using VS2012 because the XSLT debugger doesn't work in VS2015.

I've stripped down my code to the following procedure, which produces the XmlException "The empty string '' is not a valid name":

 string xsltpath = "switch-onix-tagnames-1.1.xsl";
 string xmlpath = "Testing.xml";
 var xct = new System.Xml.Xsl.XslCompiledTransform(true);
 var settings = new XmlWriterSettings();

 var xtr = new XmlTextReader(xmlpath);

 settings.OmitXmlDeclaration = false;
 settings.ConformanceLevel = ConformanceLevel.Document;
 settings.CloseOutput = false;

 using (var ms = new MemoryStream())
 {
     using (XmlWriter xw = XmlWriter.Create(ms, settings))
     {
         xct.Load(xsltpath);
         xct.Transform(xtr, xw);
         Debugger.Break();
     }
 }

The "Testing.xml" file is:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE ONIXmessage SYSTEM "http://www.editeur.org/onix/2.1/short/onix-international.dtd" >
<ONIXmessage release="2.1">
  <header>
    <m174>Some Publisher</m174>
    <m175>Some Person 978-999-9999</m175>
    <m283>[email protected]</m283>
    <m182>20160226</m182>
    <m183>Title information from Some Publisher</m183>
    <m184>eng</m184>
    <m185>01</m185>
    <m186>USD</m186>
    <m187>in</m187>
    <m193>General Trade</m193>
  </header>
  <product>
    <a001>001001-48747641</a001>
    <a002>03</a002>
    <productidentifier>
      <b221>02</b221>
      <b244>1433692376</b244>
    </productidentifier>
    <productidentifier>
      <b221>03</b221>
      <b244>9781433692376</b244>
    </productidentifier>
    <productidentifier>
      <b221>15</b221>
      <b244>9781433692376</b244>
    </productidentifier>
    <b012>DG</b012>
    <b014>EPUB</b014>
    <b211>029</b211>
    <b213>Open Ebook Format</b213>
    <b214>06</b214>
    <series>
      <b018>The Gospel Project</b018>
    </series>
    <title>
      <b202>01</b202>
      <b203>La Gran Historia, Relatos B&#237;blicos para los m&#225;s peque&#241;os, del Antiguo Testamento</b203>
    </title>
    <workidentifier>
      <b201>15</b201>
      <b244>9781433692376</b244>
    </workidentifier>
    <contributor>
      <b034>1</b034>
      <b035>A01</b035>
      <b047>Some Editorial Staff</b047>
    </contributor>
    <contributor>
      <b034>2</b034>
      <b035>A12</b035>
      <b036>Heath McPherson</b036>
      <b037>McPherson, Heath</b037>
      <b039>Heath</b039>
      <b040>McPherson</b040>
      <b044><![CDATA[<DIV><B>Heath McPherson</B> dirige un peque&ntilde;o estudio art&iacute;stico que produce ilustraciones ingeniosas y animaci&oacute;n para organizaciones en diversas localidades de los Estados Unidos. Su obra incluye <I>Timmy and Tammy&#39;s Train of Thought</I> (El tren del pensamiento de Timmy y Tammy), escrito por Oliver Chin, como as&iacute; tambi&eacute;n varios comerciales animados.<BR /><BR /><B>Heath McPherson</B> runs a small studio that creates whimsical illustrations and animation for folks across the country. His work includes <I>Timmy and Tammy&#39;s Train of Thought</I> by Oliver Chin as well a number of animated commercials.</DIV>]]></b044>
    </contributor>
    <b049>by Some Editorial Staff and Heath McPherson</b049>
    <b058>Spanish Language Edition</b058>
    <language>
      <b253>01</b253>
      <b252>spa</b252>
    </language>
    <b061>44</b061>
    <b064>REL012000</b064>
    <audience>
      <b204>01</b204>
      <b206>02</b206>
    </audience>
    <audiencerange>
      <b074>11</b074>
      <b075>04</b075>
      <b076>P</b076>
    </audiencerange>
    <audiencerange>
      <b074>17</b074>
      <b075>03</b075>
      <b076>4</b076>
      <b075>04</b075>
      <b076>4</b076>
    </audiencerange>
    <othertext>
      <d102>01</d102>
      <d103>02</d103>
      <d104><![CDATA[<DIV><I>Historias b&iacute;blicas para los m&aacute;s peque&ntilde;os (Antiguo Testamento)<BR /> Presente la Biblia a los aprendices m&aacute;s j&oacute;venes con <I>Historias b&iacute;blicas para los m&aacute;s peque&ntilde;os (Antiguo Testamento), edici&oacute;n de cart&oacute;n</I>. Incluye veinte historias del Antiguo Testamento en un nuevo formato interactivo, divertido y atractivo de relatos b&iacute;blicos para los chiquitos que empiezan a caminar.<BR /> Cada historia se caracteriza por ilustraciones en cuatro colores resaltados que, utilizadas en conjunto con la aplicaci&oacute;n &ldquo;B&amp;H Kids AR&rdquo; [realidad aumentada], cobrar&aacute;n vida con im&aacute;genes animadas y la lectura grabada por Jenna Lucado.<BR /> Historias b&iacute;blicas para los m&aacute;s peque&ntilde;os (Antiguo Testamento), edici&oacute;n de cart&oacute;n, </I>es el producto m&aacute;s reciente en la familia de libros de <I>Big Picture Interactive</I> &mdash;una serie dise&ntilde;ada para crecer con la familia y fortalecer relaciones en Cristo por medio de publicaciones centradas en la Biblia y difundidas por plataformas m&uacute;ltiples para apasionar a lectores de toda edad.</DIV>]]></d104>
    </othertext>
     <othertext>
      <d102>02</d102>
        <d103>02</d103>
        <d104><![CDATA[<DIV><I>Historias b&iacute;blicas para los m&aacute;s peque&ntilde;os (Antiguo Testamento)<BR /> Presente la Biblia a los aprendices m&aacute;s j&oacute;venes con <I>Historias b&iacute;blicas para los m&aacute;s peque&ntilde;os (Antiguo Testamento), edici&oacute;n de cart&oacute;n</I>. Incluye veinte historias del Antiguo Testamento en un nuevo formato interactivo, divertido y atractivo de relatos b&iacute;blicos para los chiquitos que empiezan a caminar.<BR /> Cada historia se caracteriza por ilustraciones en cuatro colores resaltados que, utilizadas en conjunto con la aplicaci&oacute;n &ldquo;B&amp;H Kids AR&rdquo; [realidad aumentada], cobrar&aacute;n vida con im&aacute;genes animadas y la lectura grabada por Jenna Lucado.<BR /> Historias b&iacute;blicas para los m&aacute;s peque&ntilde;os (Antiguo Testamento), edici&oacute;n de cart&oacute;n, </I>es el producto m&aacute;s reciente en la familia de libros de <I>Big Picture Interactive</I> &mdash;una serie dise&ntilde;ada para crecer con la familia y fortalecer relaciones en Cristo por medio de publicaciones centradas en la Biblia y difundidas por plataformas m&uacute;ltiples para apasionar a lectores de toda edad.<BR /><BR /> The Big Picture Interactive Bible Stories for Toddlers Old Testament<BR /> Introduce early learners to the Bible with <I>The Big Picture Interactive Bible Stories for Toddlers Old Testament Board Book</I>, featuring twenty Old Testament Bible Stories in this fun, interactive and engaging new Bible storybook designed just for toddlers.<BR /> Each story features enhanced four-color illustrations that used in conjunction with the B&amp;H Kids AR [Augmented Reality] app brings the pictures to life with pop-up imaging and read-a-long narration from Jenna Lucado.<BR /><I>The Big Picture Interactive Bible Stories for Toddlers Old Testament Board Book</I> is the latest in the family of <I>Big Picture Interactive</I> books &ndash; a series designed to grow with families and strengthen relationships in Christ through Bible-centered, multi-platform products that engage readers of all ages.<DIV>&#160;</DIV><DIV>&#160;</DIV><DIV>&#160;</DIV></DIV>]]></d104>
     </othertext>
     <othertext>
      <d102>13</d102>
      <d103>02</d103>
      <d104><![CDATA[<DIV><B>Heath McPherson</B> dirige un peque&ntilde;o estudio art&iacute;stico que produce ilustraciones ingeniosas y animaci&oacute;n para organizaciones en diversas localidades de los Estados Unidos. Su obra incluye <I>Timmy and Tammy&#39;s Train of Thought</I> (El tren del pensamiento de Timmy y Tammy), escrito por Oliver Chin, como as&iacute; tambi&eacute;n varios comerciales animados.<BR /><BR /><B>Heath McPherson</B> runs a small studio that creates whimsical illustrations and animation for folks across the country. His work includes <I>Timmy and Tammy&#39;s Train of Thought</I> by Oliver Chin as well a number of animated commercials.</DIV>]]></d104>
    </othertext>
    <imprint>
      <b241>01</b241>
      <b242>Publisher Cloud Imprint Key</b242>
      <b243>4028300</b243>
      <b079>Publisher</b079>
    </imprint>
    <publisher>
      <b291>01</b291>
      <b241>01</b241>
      <b242>Publisher Cloud Publisher Key</b242>
      <b243>4028289</b243>
      <b081>Some Publisher Group</b081>
    </publisher>
    <b394>04</b394>
    <b003>20160201</b003>
    <b087>2016</b087>
    <salesrights>
      <b089>02</b089>
      <b090>AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ BA BB BD BE BF BG BH BI BJ BL BM BN BO BQ BR BS BT BV BW BY BZ CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV CW CX CY CZ DE DJ DK DM DO DZ EC EE EG EH ER ES ET FI FJ FK FM FO FR GA GB GD GE GF GG GH GI GL GM GN GP GQ GR GS GT GU GW GY HK HM HN HR HT HU ID IE IL IM IN IO IQ IR IS IT JE JM JO JP KE KG KH KI KM KN KP KR KW KY KZ LA LB LC LI LK LR LS LT LU LV LY MA MC MD ME MF MG MH MK ML MM MN MO MP MQ MR MS MT MU MV MW MX MY MZ NA NC NE NF NG NI NL NO NP NR NU NZ OM PA PE PF PG PH PK PL PM PN PR PS PT PW PY QA RE RO RS RU RW SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR SS ST SV SX SY SZ TC TD TF TG TH TJ TK TL TM TN TO TR TT TV TW TZ UA UG UM US UY UZ VA VC VE VG VI VN VU WF WS YE YT ZA ZM ZW</b090>
    </salesrights>
    <measure>
      <c093>03</c093>
      <c094>1</c094>
      <c095>in</c095>
    </measure>
    <measure>
      <c093>03</c093>
      <c094>25.4</c094>
      <c095>mm</c095>
    </measure>
    <measure>
      <c093>08</c093>
      <c094>1.1</c094>
      <c095>lb</c095>
    </measure>
    <measure>
      <c093>08</c093>
      <c094>17.6</c094>
      <c095>oz</c095>
    </measure>
    <measure>
      <c093>08</c093>
      <c094>498.95</c094>
      <c095>gr</c095>
    </measure>
    <relatedproduct>
      <h208>13</h208>
      <productidentifier>
        <b221>15</b221>
        <b244>9781433689680</b244>
      </productidentifier>
    </relatedproduct>
    <supplydetail>
      <j137>Some Publisher</j137>
      <j268>02</j268>
      <j269>Y</j269>
      <j141>IP</j141>
      <j396>20</j396>
      <j143>20160201</j143>
      <j145>30</j145>
      <price>
        <j148>01</j148>
        <discountcoded>
          <j363>02</j363>
          <j364>0040</j364>
        </discountcoded>
        <j151>9.99</j151>
        <j152>USD</j152>
        <j161>20160127</j161>
      </price>
    </supplydetail>
  </product>
</ONIXmessage>

And the xsl file is:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:variable name="release" select="/*/@release" />

    <xsl:variable name="target">
        <xsl:choose>
            <xsl:when test="/ONIXMessage">short</xsl:when>

            <xsl:otherwise>reference</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="dtd-url">
        http://www.editeur.org/onix/
        <xsl:value-of select="$release" />

        /
        <xsl:value-of select="$target" />

        /onix-international.dtd
    </xsl:variable>

    <xsl:output method="xml" doctype-system="{$dtd-url}" />

    <xsl:template match="*">
        <xsl:variable name="target-name">
            <xsl:choose>
                <xsl:when test="$target='short'">
                    <xsl:value-of select="@shortname" />
                </xsl:when>

                <xsl:otherwise>
                    <xsl:value-of select="@refname" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        <xsl:element name="{$target-name}">
            <xsl:copy-of select="@*[not(name()='refname' or name()='shortname')]" />

            <xsl:apply-templates select="*|text()" />
        </xsl:element>
    </xsl:template>

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

I'm currently using .NET 4.6.1, but it doesn't seem to make any difference which version of .NET I use.

Am I missing something obvious here? I'm no expert on XML, so please go easy on me!

1
  • One note for anyone testing this code - the DTD at the top of the XML file is no longer available online, I've compensated for that by hosting it on my intranet and pointing to it in my HOSTS file. Commented May 2, 2016 at 2:27

1 Answer 1

1

Notice that * in your xsl:template matches all element in the input XML. Then, inside that template a new element will be created with name taken from either @shortname or @refname attribute of the matched element. As far as I can see, not all element in the XML has @shortname or @refname attribute, and this will cause the following exception to be raised upon such element is processed :

The empty string '' is not a valid name.

At least, make sure that the template only process elements that have either @shortname or @refname attribute :

<xsl:template match="*[@shortname or @refname]">
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that's exactly the workaround I was looking for. Unfortunately it doesn't appear to have fixed the actual problem, however - the transform runs OK, but the resulting XML is pretty much unchanged from the original. I think the issue may be that the DTD isn't loading properly, with the result that the @shortname isn't being matched on any of the nodes. I'm attempting to use an XSD instead, hopefully that will make a difference.
I've just tried running Fiddler while doing the transform, and there is no indication that the DTD is being loaded at all. Hmmm...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.