Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: XSLT question: How to lookup another tag's children in XSLT

From: "roy axenov" <r_axenov@----.-->
To: NULL
Date: 10/1/2006 12:35:00 AM


Please don't top-post.

yinglcs@g... wrote:
> For example, for this input:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <root>
>     <feature name="A">
>         <depends name="A1"/>
>         <depends name="A2"/>
>     </feature>
>
>     <feature name="B">
>         <depends name="B1"/>
>     </feature>
>
>     <feature name="C">
>         <depends name="C1"/>
>         <depends name="C2"/>
>     </feature>
>
>     <features>
>         <feature name="A"/>
>         <feature name="B"/>
>     </features>
> </root>
>
> I would like to generate this output:
>  Feature A Depends on A1
>  Feature A Depends on A2
>
> Feature B Depends on B1
>
> Here is my xslt:
>
> <xsl:stylesheet version="1.0"
>     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

What about the output method? (And XML declaration, for
that matter.) Since you seem to be trying to output text,
perhaps you should give a hint about that to your XSLT
processor - and to other people's XSLT processors?

>     <xsl:template match="/features">
>         <xsl:for-each select="feature">

Why?

>         <xsl:call-template name="feature">
>         </xsl:call-template>

I don't believe that's what <xsl:call-template> is for.

>         </xsl:for-each>
>     </xsl:template>
>
>     <xsl:template name="feature">

And what's that supposed to match?

>         <xsl:param name="dummy" />
>             <xsl:text>Feature </xsl:text>
>               <xsl:value-of select="@name" />
>             <xsl:text>Depends on </xsl:text>
>         </xsl:if>

That's not even well-formed.

>     </xsl:template>
>
>     <xsl:template match="depends">
>         <xsl:value-of select="@name" />
>         <xsl:text> </xsl:text>
>     </xsl:template>

And how's that supposed to work?

> </xsl:stylesheet>
>
> But this just match each 'feature' element in the source.

No, it matches all the <depends> elements in your XML,
because your third template is the only one that actually
does anything. Try removing the first two templates, and
you'll get exactly the same result.

> 1. if I do this:
> <xsl:template name="feature"> ... </xsl:template>
> intead of
> <xsl:template match="feature"> ... </xsl:template>
>
> I don't match any 'feature' element.

Precisely. You don't match anything. So the template
doesn't do anything.

> But the output show that i only match the 'feature'.

No, it doesn't. It shows something entirely different. See
above.

> Here is the xslt output.  That is not i would like
> to accomplish.
>         A1
>         A2
>
>
>
>         B1
>         B2
>
>
>
>         C1
>         C2

For that matter, it's NOT the XSLT output, either. First,
your XSLT is not well-formed, and even if it were
well-formed, there's no sign of B2 anywhere in the XML.
(But that's to be expected; B2's are supposed to be
awfully stealthy, after all.)

If you really want to get help on the newsgroup, I'd
heartily recommend posting your actual code, as well as
actual examples of your input/output. It seems just a bit
disrespectful -- you're asking people for help, but you
can't be bothered to weed out even the most obvious bugs
(such as that </xsl:if>) in your code.

No one's asking you to post your production code or
anything. Just spend a minute or two making sure what
you're posting is relevant to your question. I spent five
minutes of my Sunday morning straightening out your WAG's,
and if I started tinkering with your code in a pretty
cheerful mood, I'm now feeling quite murderous.

> 2. Why this does not match anything? I expect this will
> loop thru each children of "features" and call the name
> template 'feature'. But apparently, I did something
> wrong.

That's not how it works. But since you couldn't be bothered
to double-check what you post, I can't be bothered to
explain that in detail to you. Read the spec. Or the
reference. Whatever.

The following works, but you'll just have to figure out why
yourself. It'll do you a world of good, I think.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/features">
    <xsl:apply-templates select="feature"/>
  </xsl:template>
  <xsl:template match="features/feature">
    <xsl:apply-templates
      select="/root/feature[@name=current()/@name]/depends"
      mode="list-dependencies">
      <xsl:with-param name="p-node" select="."/>
    </xsl:apply-templates>
    <xsl:text>&#010;</xsl:text>
  </xsl:template>
  <xsl:template match="depends" mode="list-dependencies">
    <xsl:param name="p-node"/>
    <xsl:text>Feature </xsl:text>
    <xsl:value-of select="$p-node/@name"/>
    <xsl:text> depends on </xsl:text>
    <xsl:value-of select="@name"/>
    <xsl:text>&#010;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

-- 
roy axenov



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent