One pesky blank line in an XSLT transform

怎甘沉沦 提交于 2019-12-11 03:08:22

问题


I'm using XSLT to extract some data from a trademark XML file from the Patent and Trademark Office. It's mostly okay, except for one blank line. I can get rid of it with a moderately ugly workaround, but I'd like to know if there a better way.

Here's a subset of my XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tm="http://www.wipo.int/standards/XMLSchema/trademarks" xmlns:pto="urn:us:gov:doc:uspto:trademark:status">
<xsl:output method="text" encoding="utf-8" />
<xsl:strip-space elements="*"/>
<xsl:template match="tm:Transaction">
<xsl:apply-templates select=".//tm:TradeMark"/>
<xsl:apply-templates select=".//tm:ApplicantDetails"/>
<xsl:apply-templates select=".//tm:MarkEvent"/>
</xsl:template>

<xsl:template match="tm:TradeMark">
MarkCurrentStatusDate,"<xsl:value-of select="normalize-space(tm:MarkCurrentStatusDate)"/>"<xsl:text/>
ApplicationNumber,"<xsl:value-of select="normalize-space(tm:ApplicationNumber)"/>"<xsl:text/>
ApplicationDate,"<xsl:value-of select="normalize-space(tm:ApplicationDate)"/>"<xsl:text/>
RegistrationNumber,"<xsl:value-of select="normalize-space(tm:RegistrationNumber)"/>"<xsl:text/>
RegistrationDate,"<xsl:value-of select="normalize-space(tm:RegistrationDate)"/>"<xsl:text/>
<xsl:apply-templates select="tm:WordMarkSpecification"/>
<xsl:apply-templates select="tm:TradeMarkExt"/>
<xsl:apply-templates select="tm:PublicationDetails"/>
<xsl:apply-templates select="tm:RepresentativeDetails"/>
</xsl:template>

<xsl:template match="tm:WordMarkSpecification">
MarkVerbalElementText,"<xsl:value-of select="normalize-space(tm:MarkVerbalElementText)"/>"<xsl:text/>
</xsl:template>

It has a few more templates, but that's the gist of it. I always get a blank line at the very beginning of the output, before any data; I don't get any other blank lines. My circumvention is to combine the two lines:

<xsl:template match="tm:TradeMark">
MarkCurrentStatusDate,"<xsl:value-of select="normalize-space(tm:MarkCurrentStatusDate)"/>"<xsl:text/>

into a single line:

<xsl:template match="tm:TradeMark">MarkCurrentStatusDate,"<xsl:value-of select="normalize-space(tm:MarkCurrentStatusDate)"/>"<xsl:text/>

This works, and I guess I'm okay with it if there's nothing better, but it seems inelegant and like a kludge to me. None of the other templates need this treatment (e.g. the tm:WordMarkSpecification template or another six after that)), and I'm confused why it's needed here. Any ideas?

Because I can see the specific point in the XSLT that's inserting the blank line, I presume it's not helpful to provide the XML I'm testing on, but if you do need to see it, you can get it at https://tsdrapi.uspto.gov/ts/cd/casestatus/rn2178784/download.zip ; it's the XML file in that archive.


回答1:


Use at the beginning of the template the same trick you are using at the end of the template to chop up the stylesheet node tree with empty <xsl:text/> instructions:

<xsl:template match="tm:TradeMark">
<xsl:text/>MarkCurrentStatusDate,"<xsl:value-of select="normalize-space(tm:MarkCurrentStatusDate)"/>"<xsl:text/>



回答2:


Personally, I think it's cleaner to use a concat() when you need to combine static text and dynamic values:

<xsl:template match="tm:TradeMark">
  <xsl:value-of 
   select="concat(
   'MarkCurrentStatusDate,&quot;', normalize-space(tm:MarkCurrentStatusDate), '&quot;',
   'ApplicationNumber,&quot;', normalize-space(tm:ApplicationNumber), '&quot;',
   'ApplicationDate,&quot;', normalize-space(tm:ApplicationDate), '&quot;',
   'RegistrationNumber,&quot;', normalize-space(tm:RegistrationNumber), '&quot;',
   'RegistrationDate,&quot;', normalize-space(tm:RegistrationDate), '&quot;'
    )"/>
  <xsl:apply-templates select="tm:WordMarkSpecification"/>
  <xsl:apply-templates select="tm:TradeMarkExt"/>
  <xsl:apply-templates select="tm:PublicationDetails"/>
  <xsl:apply-templates select="tm:RepresentativeDetails"/>
</xsl:template>

This should also solve your issue with the blank spaces showing up.




回答3:


Remember you can always just play with the XML syntax to ignore end-of-line sequences that are inside of start and end tag delimiters:

<xsl:template match="tm:TradeMark"
>MarkCurrentStatusDate,"<xsl:value-of select="normalize-space(tm:MarkCurrentStatusDate)"
/>"ApplicationNumber,"<xsl:value-of select="normalize-space(tm:ApplicationNumber)"
/>"ApplicationDate,"<xsl:value-of select="normalize-space(tm:ApplicationDate)"
/>"RegistrationNumber,"<xsl:value-of select="normalize-space(tm:RegistrationNumber)"
/>"RegistrationDate,"<xsl:value-of select="normalize-space(tm:RegistrationDate)"
/>"<xsl:apply-templates select="tm:WordMarkSpecification"/>
<xsl:apply-templates select="tm:TradeMarkExt"/>
<xsl:apply-templates select="tm:PublicationDetails"/>
<xsl:apply-templates select="tm:RepresentativeDetails"/>
</xsl:template>

There is no rule in XML that a tag's closing delimiter /> has to be on the same line as the tag's opening delimiter <. White-space inside of a tag is ignored (where innocuous), and an end-of-line sequence is considered white-space.




回答4:


If you want all of the text to be emitted without extra line breaks or white-space, then put the literal text inside of <xsl:text> elements.

<xsl:template match="tm:TradeMark">
    <xsl:text>MarkCurrentStatusDate,"</xsl:text>
        <xsl:value-of select="normalize-space(tm:MarkCurrentStatusDate)"/>
    <xsl:text>"</xsl:text>
    <xsl:text>ApplicationNumber,"</xsl:text>
        <xsl:value-of select="normalize-space(tm:ApplicationNumber)"/>
    <xsl:text>"</xsl:text>
    <xsl:text>ApplicationDate,"</xsl:text>
        <xsl:value-of select="normalize-space(tm:ApplicationDate)"/>
    <xsl:text>"</xsl:text>
    <xsl:text>RegistrationNumber,"</xsl:text>
        <xsl:value-of select="normalize-space(tm:RegistrationNumber)"/>
    <xsl:text>"</xsl:text>
    <xsl:text>RegistrationDate,"</xsl:text>
        <xsl:value-of select="normalize-space(tm:RegistrationDate)"/>
    <xsl:text>"</xsl:text>
    <xsl:apply-templates select="tm:WordMarkSpecification"/>
    <xsl:apply-templates select="tm:TradeMarkExt"/>
    <xsl:apply-templates select="tm:PublicationDetails"/>
    <xsl:apply-templates select="tm:RepresentativeDetails"/>
</xsl:template>

That way, none of the line breaks and white-space inside of the <xsl:template> will be seen as significant and will not be included in the result tree output.



来源:https://stackoverflow.com/questions/17823425/one-pesky-blank-line-in-an-xslt-transform

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!