Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a datafile like this:

<Key name="com.ahsay.afc.cpf.UserGroup" content="" allowMultiple="Y">
    <Value name="rsv-id" inheritParentAttribute="Y" type="string" data="1328200856753" />
    <Value name="rsv-group-name" inheritParentAttribute="Y" type="string" data="group 1" />
    <Value name="rsv-user-type" inheritParentAttribute="Y" type="string" data="backup-user" />
    <Value name="rsv-owner" inheritParentAttribute="Y" type="string" data="" />
    <Key name="com.ahsay.afc.cpf.User" content="" allowMultiple="Y">
        <Value name="rsv-id" inheritParentAttribute="Y" type="string" data="13279083887401" />
        <Value name="rsv-login-name" inheritParentAttribute="Y" type="string" data="name1" />
    </Key>
    <Key name="com.ahsay.afc.cpf.User" content="" allowMultiple="Y">
        <Value name="rsv-id" inheritParentAttribute="Y" type="string" data="13279083887401" />
        <Value name="rsv-login-name" inheritParentAttribute="Y" type="string" data="name2" />
    </Key>
</Key>
<Key name="com.ahsay.afc.cpf.UserGroup" content="" allowMultiple="Y">
    <Value name="rsv-id" inheritParentAttribute="Y" type="string" data="1328200856753" />
    <Value name="rsv-group-name" inheritParentAttribute="Y" type="string" data="group 2" />
    <Value name="rsv-user-type" inheritParentAttribute="Y" type="string" data="backup-user" />
    <Value name="rsv-owner" inheritParentAttribute="Y" type="string" data="" />
    <Key name="com.ahsay.afc.cpf.User" content="" allowMultiple="Y">
        <Value name="rsv-id" inheritParentAttribute="Y" type="string" data="13279083887401" />
        <Value name="rsv-login-name" inheritParentAttribute="Y" type="string" data="name3" />
    </Key>
    <Key name="com.ahsay.afc.cpf.User" content="" allowMultiple="Y">
        <Value name="rsv-id" inheritParentAttribute="Y" type="string" data="13279083887401" />
        <Value name="rsv-login-name" inheritParentAttribute="Y" type="string" data="name4" />
    </Key>
</Key>

I know the login name of the record I want, and I need to match it to a group. Let's say I want to know what group name3 is (the answer is group 3). Currently I can get the name of the group out of the file with:

perl -ne 'print "$_\n" foreach /name="rsv-group-name".*\ data="([^"]*)"/g;'

but I have no idea how to match it with a user. How can I do that in a script?

share|improve this question

2 Answers 2

Look into usling xslt scripts to transform or parse your XML, xsltproc is the binary you are looking for and w3cschools have a good getting started guide.

share|improve this answer

Ok xlst looks good but since i have all the key's named the same i still fail do mach name to group

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="umm" match="Value" use="@data"/>
<xsl:key name="amm" match="Value" use="@name"/>
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="key('umm','name3')">
   <xsl:for-each select="key('amm','rsv-group-name')">
<p>
<xsl:value-of select="@data"/>
</p>
</xsl:for-each>
</xsl:for-each>
</body>
</html>
</xsl:template>

will produce:

group 1

group 2

share|improve this answer
1  
If this XLST doesn't answer your question, please don't post this as an answer. Details of XSLT transforms are off-topic here, but you could as a separate question on stackoverflow.com detailing your source xml, what you want, and what you have tried (i.e. what you posted as an answer here). –  Mat Feb 3 '12 at 11:27
    
Thanks and sorry. –  Miq Feb 3 '12 at 12:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.