views:

33

answers:

2

I've got some *.xml with elements like :

<app-method name="leave-accrual-status-details" kind="enquiry">
    <title>...</title>
    <required-roles>
      <role name="authenticated"/>
    </required-roles>
    <asd>
      <param name="..." datatype="dt:int" control="hidden" call-kind="..." data-kind="..."/>
    </asd>
    <data-engine sp="spLeaveAccrualStatusDetails">
    ...
    </data-engine>
    <wia>
     ...
    </wia>
  </app-method>

And generating new .xml-document (selecting only "app-method" elements).I'm doing it this way : (.xsl)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  <xsl:output method="xml" encoding="utf-8" indent="no"></xsl:output>

  <xsl:template match="/">
  <xsl:for-each select="//app-method">
  <xsl:if test='./required-roles/role[@name="administrator"]'>
  <xsl:copy-of select="." />
  </xsl:if>
  </xsl:for-each>
  </xsl:template>

 </xsl:stylesheet>

It's working perfectly, but.. Is there any way to do it without "xsl:copy-of"? Think I need to use something like "node-name('blabla')" and value-of?.. And how to select a set of methods parameters (distinct)? ( param name="parameter name" ), yeah I need to use "key" but can't find good samples with it

Many thanks

P.S When converting xml->xml I have to use Far("xsl in.xml transform.xsl out.xml"),'cause there is some strange error when trying to do it from cdm.exe, I dislike FAR a little bit,is there another way to convert xmls?

+2  A: 

It's working perfectly, but.. Is there any way to do it without "xsl:copy-of"? Think I need to use something like "node-name('blabla')" and value-of?..

Read about the <xsl:element> instruction and also about the identity rule.

.. And how to select a set of methods parameters (distinct)? ( param name="parameter name" ), yeah I need to use "key" but can't find good samples with it

This is a second question (out of three) and very different than the first. Read about Muenchian grouping. And ask it as a separate question.

Dimitre Novatchev
Thank,I've already done distinct,It was pretty easy
taras.roshko
Now reading about <xsl:element /> and <xsl:attribute>
taras.roshko
+1  A: 

Distinct

    <xsl:key name="pkey" match="param" use="@name"/>

     <xsl:template match="/">
        <xsl:for-each select='//app-method/asd/param[generate-id() = generate-id(key("pkey", @name)[1])]'>
             <parameter>
             <xsl:value-of select="@name"/>
             </parameter>
        </xsl:for-each>
     </xsl:template>
</xsl:stylesheet>

Unique

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:key name="pkey" match="@name" use="."/>

     <xsl:template match="/">
        <xsl:for-each select="//app-method/asd">
             <xsl:copy-of select='param[@name[generate-id() = generate-id(key("pkey",.)[1])]]'/>
        </xsl:for-each>
     </xsl:template>
</xsl:stylesheet>

Using cmd.exe to convert XML : download MSXSL.exe (newest) put it in some system folder (that already is included in path variable) and then from command line msxsl source.xml transformer.xsl -o result.xml

I'll Write tomorrow about copying without "copy-of"

BTW : good Tutorial

taras.roshko
What do you mean by the difference between Distinct and Unique? Aren't your code snippets doing the same thing, aside from the output format difference, `<parameter>foo</parameter>` vs.`<param name="foo">...</param>`?
LarsH