0

I am migrating an XSL translation and the new XML input no longer has an XML namespace.

The XSL below used to output "Test Headline" but now produces nothing.

Old XML:

<article xmlns:as="http://www.company.com/schemas/articleschema">
  <parastyles>
    <parastyle name="Body Copy" uid="292"/>
    <parastyle name="Headline Regular" uid="311"/>
  </parastyles>
  <charstyles>
    <charstyle name="Character Style 1" uid="285"/>
  </charstyles>
  <story name="head">
    <runs>
      <run p="311" c="285">Test Headline</run>
    </runs>
  </story>
  <story name="body">
    <runs>
      <run p="292" c="285">
        <eol hyphenated="true"/>Met facipsam lam sinti do<eol/>lorendae dem endae lita sim
        <eol hyphenated="true"/>fuga. Nam, ut que venda es si<eol/>tame nimin con ex exerisque
      </run>
    </runs>
  </story>
</article>

New XML:

<article xmlns="http://www.company.com/schemas/articleschema">
  <parastyles>
    <parastyle name="Body Copy" uid="292"/>
    <parastyle name="Headline Regular" uid="311"/>
  </parastyles>
  <charstyles>
    <charstyle name="Character Style 1" uid="285"/>
  </charstyles>
  <story name="head">
    <runs>
      <run p="311" c="285">Test Headline</run>
    </runs>
  </story>
  <story name="body">
    <runs>
      <run p="292" c="285">
        <eol hyphenated="true"/>Met facipsam lam sinti do<eol/>lorendae dem endae lita sim
        <eol hyphenated="true"/>fuga. Nam, ut que venda es si<eol/>tame nimin con ex exerisque
      </run>
    </runs>
  </story>
</article>

The only difference is the change in namespace: xmlns:as

Here is my XSL that works on the old XML but not on the new XML:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <xsl:apply-templates select="//story[@name='head']"/>
    </xsl:template>
    <xsl:template match="text()">
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>
    <xsl:template match="eol[@hyphenated]">
        <xsl:text></xsl:text>
    </xsl:template>
    <xsl:template match="eol">
        <xsl:text> </xsl:text>
    </xsl:template>
</xsl:stylesheet>

I'm sure it's something ridiculously simple, but I'm not familiar enough with namespaces to figure it out. Thanks in advance!

Update -- XSL Solution:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:as="http://www.company.com/schemas/articleschema"
exclude-result-prefixes="as">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <xsl:apply-templates select="//as:story[@name='head']"/>
    </xsl:template>
    <xsl:template match="text()">
        <xsl:value-of select="normalize-space(.)"/>
    </xsl:template>
    <xsl:template match="eol[@hyphenated]">
        <xsl:text></xsl:text>
    </xsl:template>
    <xsl:template match="eol">
        <xsl:text> </xsl:text>
    </xsl:template>
</xsl:stylesheet>
CMarcera
  • 69
  • 7
  • 2
    *"The only difference is the change in namespace: xmlns:as"* That's a major difference, because it puts all elements in the XML inside the declared namespace (previously, the namespace declaration was redundant, since there are no elements with an `as` prefix). See here how to proceed: https://stackoverflow.com/a/34762628/3016153 – michael.hor257k Aug 10 '22 at 18:00
  • Following your example, I was able to cobble together XSL that works, updated in OP. Though I honestly don't understand why the XSL worked in the original example with a namespace added, 'as', but didn't work without a namespace. It seems like I had to add a non-existent namespace to my new XSL and include it? Regardless, it's working so I thank you :) Just wish I understood why... – CMarcera Aug 10 '22 at 21:46
  • As I already mentioned, your original XSLT worked with the old XML because the namespace declaration in the old XML didn't do anything. You could have removed it and nothing would have changed. It would only have an effect if there were an element named `as:something`. --- Note also that your 2 templates matching `eol` also do not do anything, because no templates are applied to the `eol` elements. If you did apply templates to them and wanted them to take effect, you would also need to use the `as` prefix in the match pattern. – michael.hor257k Aug 10 '22 at 22:19

0 Answers0