Forum Moderators: open
I need to filter values in some attributes and I can't manage to get it right. It's the values in top, left and Area that I need to filter so it looks like this; top="20" left="30" and <Area unit="cm2">360</Area>
This is the xml
<PressItem>
<Contents>
<Content>
<Section seqno="01" />
<Page seqno="220221" pagecount="5,6" />
<PageSize unit="" width="" height="14.36" />
<PagePosition unit="" top="10,20" left="30,40" />
<Area unit="cm2">360,370</Area>
<Headline>Something</Headline>
<Byline />
<Introduction />
<Body />
<Pictures>
<captions />
</Pictures>
</Content>
</Contents>
<PressItem>
The xsl looks like this
<xsl:template match="OriginalMetaXml/PressItem">
<xsl:apply-templates select="*[not(self::ArticleIdentifier存elf::ContentFiles存elf::FirstValidDateTime存elf::LastValidDateTime存elf::PostProcessedBy)]" mode="copy"/>
</xsl:template>
<xsl:template match="*" mode="copy">
<xsl:copy>
<xsl:copy-of select="@*[name() != 'pagecount' and name() != 'pagespread']"/>
<xsl:apply-templates mode="copy"/>
</xsl:copy>
</xsl:template>
I have been told to use the code below but I don't really know how to use this code in the xsl. Can anyone help me with this?
<xsl:choose>
<xsl:when test="contains(.,',')"><xsl:value-of select="substring-before(.,',')"/></xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
</xsl:choose>
[edited by: encyclo at 6:03 pm (utc) on May 18, 2008]
[edit reason] disactivated smilies [/edit]
<xsl:choose>
<xsl:when test="contains(.,',')">
<xsl:value-of select="substring-before(.,',')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
contains(.,',') = "this node value"
contains(.,',') = "contains a comma"
substring-before(.,',') = "return everything before the comma."
select="." = "just return the entire node value"
In the example below I want top to be 23 and Area to be 628.
<Contents>
<Content>
<Section seqno="01" />
<Page seqno="4">4</Page>
<PageSize unit="mm" width="275" height="395" />
<PagePosition unit="mm" top="23,23" left="113,0" />
<Area unit="cm">628,714</Area>
The xsl looks like this. The filtering of the Area tag works fine but nothing happens to the top attribute. Why is that and what should I do instead?
<xsl:template match="OriginalMetaXml/PressItem">
<xsl:apply-templates select="*[not(self::ArticleIdentifier存elf::ContentFiles存el f::FirstValidDateTime存elf::LastValidDateTime存elf ::PostProcessedBy)]" mode="copy"/>
</xsl:template>
<xsl:template match="*共*" mode="copy">
<xsl:copy>
<xsl:copy-of select="@*[name() != 'pagearray' and name() != 'spread']"/>
<xsl:apply-templates mode="copy"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@top再rea" mode="copy">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:choose>
<xsl:when test="contains(.,',')">
<xsl:value-of select="substring-before(.,',')"/>
</xsl:when>
<xsltherwise>
<xsl:value-of select="."/>
</xsltherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
You have a typo:
<xsl:template match="@top再rea" mode="copy">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:choose>
<xsl:when test="contains(.,',')">
<xsl:value-of select="substring-before(.,',')"/>
</xsl:when>
<xsltherwise>
<xsl:value-of select="."/>
</xsltherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
That's probably caused by this forum's propensity for turning :o into a smiley. Don't forget this forum also scrags the pipe character (¦ is not a pipe). It does this for security reasons.
I'm also not sure why you're using copy and copy-of. I think that may be an issue.
I'd highly recommend using <oXygen/> to test this. I would find it impossible to write XSLT without it. You could also use Altova XML Spy or Stylus Studio, but they are both programs costing hundreds, while <oXygen/> costs less than fifty bucks.