Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Re: match if an attribute has a value, or is not available.

Reply
Thread Tools

Re: match if an attribute has a value, or is not available.

 
 
Dimitre Novatchev
Guest
Posts: n/a
 
      07-05-2003
> <xsl:template match="*[@xml:lang = 'ES' or not
> attribute-available(@xml:lang)]" />
> </xsl:stylesheet>
>
> I need the equivalent of the function attribute-available(). Other
> permutations of this filter naturally result in no nodes in the output XML


*[@xml:lang='ES' or not(@xml:lang)]


or


*[not(@xml:lang and @xml:lang != 'ES') ]




=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL


 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      07-05-2003
> *[@xml:lang='ES' or not(@xml:lang)]

> or


> *[not(@xml:lang and @xml:lang != 'ES') ]


Thanks, but (given 'xsltproc oneLang.xslt manyLang.xml'), the second returns
only tags with no xml:lang at all, and the first returns no nodes
whatsoever.

(Using libxml 20507, libxslt 10030 and libexslt 720
xsltproc was compiled against libxml 20507, libxslt 10030 and libexslt 720
libxslt 10030 was compiled against libxml 20507
libexslt 720 was compiled against libxml 20507)

BTW I have solved my outer problem with this script:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/|@*|node()">
<xsl:copy>
<xsl:apply-templates
select="(@*|node())[descendant-or-self::*[lang($copy-lang)] or
not(@xml:lang)]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

But it's still not as minimal as I would have liked. One uses it like this:

xsltproc --param copy-lang "'es'" oneLang.xslt manyLang.xml

--
Phlip
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces


 
Reply With Quote
 
 
 
 
Dimitre Novatchev
Guest
Posts: n/a
 
      07-05-2003

"Phlip" <(E-Mail Removed)> wrote in message
news:be7526$(E-Mail Removed)...
> > *[@xml:lang='ES' or not(@xml:lang)]

>
> > or

>
> > *[not(@xml:lang and @xml:lang != 'ES') ]

>
> Thanks, but (given 'xsltproc oneLang.xslt manyLang.xml'), the second

returns
> only tags with no xml:lang at all, and the first returns no nodes
> whatsoever.


I cannot reproduce this. With 8 different XSLT processors and with this
transformation:

<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/">
/*/*[@xml:lang='ES' or not(@xml:lang)]
<xsl:copy-of select="/*/*[@xml:lang='ES' or not(@xml:lang)]"/>

/*/*[not(@xml:lang and @xml:lang != 'ES') ]
<xsl:copy-of select="/*/*[not(@xml:lang and @xml:lang != 'ES') ]"/>

</xsl:template>
</xsl:stylesheet>


when applied on your source.xml:

<gloss>
<note>has no language, so this remains</note>
<verbiage xml:lang="EN">dog</verbiage>
<verbiage xml:lang="ES">perro</verbiage>
<verbiage xml:lang="PT">cao</verbiage>
</gloss>

The same (wanted) result is produced:


/*/*[@xml:lang='ES' or not(@xml:lang)]
<note>has no language, so this remains</note><verbiage
xml:lang="ES">perro</verbiage>

/*/*[not(@xml:lang and @xml:lang != 'ES') ]
<note>has no language, so this remains</note>
<verbiage xml:lang="ES">perro</verbiage>


>
> (Using libxml 20507, libxslt 10030 and libexslt 720
> xsltproc was compiled against libxml 20507, libxslt 10030 and libexslt 720
> libxslt 10030 was compiled against libxml 20507
> libexslt 720 was compiled against libxml 20507)


One of these 8 XSLT processors is xsltProc (libxml 20504, libxslt 10027.
libexslt 71

>
> BTW I have solved my outer problem with this script:
>
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0"
> xmlnssl="http://www.w3.org/1999/XSL/Transform">
> <xsl:template match="/|@*|node()">
> <xsl:copy>
> <xsl:apply-templates
> select="(@*|node())[descendant-or-self::*[lang($copy-lang)] or
> not(@xml:lang)]"/>


This is incorrect -- see below.


> </xsl:copy>
> </xsl:template>
> </xsl:stylesheet>
>
> But it's still not as minimal as I would have liked. One uses it like

this:
>



In your first message you said "The source has many languages,
and the target only one."

Your solution, when applied with $copy-lang set to "ES" on this source.xml:

<gloss>
<note lang="CH">
This has a language different from ES
and must not be copied!!!
<subnote xml:lang="ES">perro</subnote>
<subnote xml:lang="EN">dog</subnote>
</note>
<verbiage xml:lang="EN">dog</verbiage>
<verbiage xml:lang="ES">perro</verbiage>
<verbiage xml:lang="PT">cao</verbiage>
</gloss>

produces this result:

<?xml version="1.0" encoding="UTF-16"?><gloss>
<note lang="CH">
This has a language different from ES
and must not be copied!!!
<subnote xml:lang="ES">perro</subnote>

</note>

<verbiage xml:lang="ES">perro</verbiage>

</gloss>


The "note" element is in the "CH" language, and the result is in two
languages ("CH" and "ES"), not in one.



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL


 
Reply With Quote
 
Marrow
Guest
Posts: n/a
 
      07-05-2003
Hi Phlip,

I'm not sure if this will apply to the XML documents you are filtering - but
be wary that the xml:lang also applies to descendants of the element on
which it is declared where it is not overridden on those descendants.

For example, the following XML...

<?xml version="1.0"?>
<gloss xml:lang="EN">
<note>has no language - but is a descendant of one that does</note>
<verbiage xml:lang="EN">dog</verbiage>
<verbiage xml:lang="ES">perro</verbiage>
<verbiage xml:lang="PT">cao</verbiage>
</gloss>

The language of the <note> element is now 'EN' because it desecends from
<gloss> which is defined as 'EN'. So your current XSLT will copy that
<note> even though it is not 'ES'. I think maybe a slight tweak to your
XSLT should prevent that from happening...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslaram name="copy-lang" select="'ES'"/>
<xsl:template match="/|@*|node()">
<xsl:copy>
<xsl:apply-templates
select="(@*|node())[descendant-or-self::*[lang($copy-lang)] or
(not(@xml:lang) and lang($copy-lang))]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Cheers
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator

"Phlip" <(E-Mail Removed)> wrote in message
news:be7526$(E-Mail Removed)...
> > *[@xml:lang='ES' or not(@xml:lang)]

>
> > or

>
> > *[not(@xml:lang and @xml:lang != 'ES') ]

>
> Thanks, but (given 'xsltproc oneLang.xslt manyLang.xml'), the second

returns
> only tags with no xml:lang at all, and the first returns no nodes
> whatsoever.
>
> (Using libxml 20507, libxslt 10030 and libexslt 720
> xsltproc was compiled against libxml 20507, libxslt 10030 and libexslt 720
> libxslt 10030 was compiled against libxml 20507
> libexslt 720 was compiled against libxml 20507)
>
> BTW I have solved my outer problem with this script:
>
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0"
> xmlnssl="http://www.w3.org/1999/XSL/Transform">
> <xsl:template match="/|@*|node()">
> <xsl:copy>
> <xsl:apply-templates
> select="(@*|node())[descendant-or-self::*[lang($copy-lang)] or
> not(@xml:lang)]"/>
> </xsl:copy>
> </xsl:template>
> </xsl:stylesheet>
>
> But it's still not as minimal as I would have liked. One uses it like

this:
>
> xsltproc --param copy-lang "'es'" oneLang.xslt manyLang.xml
>
> --
> Phlip
> http://www.c2.com/cgi/wiki?TestFirstUserInterfaces
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
re.sub(): replace longest match instead of leftmost match? John Gordon Python 13 12-20-2011 02:58 AM
pat-match.lisp or extend-match.lisp in Python? ekzept Python 0 08-10-2007 06:08 PM
ATTRIBUTE ERROR: 'module' object has no attribute 'ssl' johnny Python 5 12-10-2006 01:10 PM
$match = true() for empty $match?? Victor XML 2 05-17-2004 10:43 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments