0

I am new with XSLT and I also have developed an Audit Trail system, each action(e.g Edit,Add New and Remove) will be track for each object, I have inserted OldValue and NewValue of object as XML into DB

In UI I will show user the audit details as following pic:

enter image description here

But I have decided show 'Full Name' instead of 'FullName' or 'Calendar Name' instead of 'CalendarName' or in future I would like use multiple language for captions.

Generated XML of OldValue is:

   <BasicInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <UserIdentify>100</UserIdentify>
  <UserName>user1</UserName>
  <FullName>Ali Hassanabadi</FullName>
  <Email>ali.h@test.com</Email>
  <LogInCounter>0</LogInCounter>
  <Title>Dr.</Title>
  <Label />
  <Theme>dark-hive</Theme>
  <Language>English</Language>
  <CalendarName>Gregorian</CalendarName>
  <ExpireDate xsi:nil="true" />
  <Status>1</Status>
</BasicInfo>

So XSLT file can be use to convert XML to XHTML, but the problem is I have a lot of classes they are using Auditing system. I think this is not good to define XSLT file for each class so I came to this conclusion to define custom attribute for each property which specified property's caption, something like this:

public class BasicInfo  : IAuditable
{
    [AuditingTitle("Full Name")]
    public string FullName
    {
        get;
        set;
    }
}

now when the Old Value or New Value are showing, I find class name and it's properties by the calling assembly name as well as for each property I must find it's attribute title by the reflections.

Something like this:

   <?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/Found"> 
        <div>
          <b>Here bind to found title of properties</b>
          <xsl:value-of select="PropertyValue"/>
        </div> 
  </xsl:template>
</xsl:stylesheet>

So is there a way to define one XSLT(dynamic) compatible with all generated XMLs ?

Thanks in advance.

Aria
  • 3,724
  • 1
  • 20
  • 51

1 Answers1

0

I'm not quite sure if I interpret your question correctly but if this is just about turning your </BasicInfo> "record" into a key-value-pair structure using the XML tag names as keys this is a possible way to go in XSLT 1.0 with entityname being passed to the XSLT process from the outside:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

  <xsl:param name="entityname" select="'BasicInfo'"/>

  <xsl:template match="/*">
    <xsl:if test="local-name() = $entityname">
      <tab>
        <xsl:for-each select="*">
          <row>
            <div><xsl:value-of select="local-name()"/></div>
            <div><xsl:value-of select="."/></div>
          </row>
        </xsl:for-each>
      </tab>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Note that this approach is a little clumsy since XSLT 1.0 does not permit the use of parameters/variables in match expressions (see How to use a parameter in a xslt as a XPath?).

The XSLT will generate the following output:

<?xml version="1.0" encoding="UTF-8"?>
<tab>
  <row>
    <div>UserIdentify</div>
    <div>100</div>
  </row>
  <row>
    <div>UserName</div>
    <div>user1</div>
  </row>
  <row>
    <div>FullName</div>
    <div>Ali Hassanabadi</div>
  </row>
  <row>
    <div>Email</div>
    <div>ali.h@test.com</div>
  </row>
  <row>
    <div>LogInCounter</div>
    <div>0</div>
  </row>
  <row>
    <div>Title</div>
    <div>Dr.</div>
  </row>
  <row>
    <div>Label</div>
    <div/>
  </row>
  <row>
    <div>Theme</div>
    <div>dark-hive</div>
  </row>
  <row>
    <div>Language</div>
    <div>English</div>
  </row>
  <row>
    <div>CalendarName</div>
    <div>Gregorian</div>
  </row>
  <row>
    <div>ExpireDate</div>
    <div/>
  </row>
  <row>
    <div>Status</div>
    <div>1</div>
  </row>
</tab>
Community
  • 1
  • 1
Marcus Rickert
  • 4,138
  • 3
  • 24
  • 29
  • Thank you,Good I got one step closer, but how can I define one XSLT to generate output for all XML format (I mean change match at run time) ? How about nested XML ? – Aria Nov 02 '14 at 06:09
  • See my updated answer as far as choosing the entity name at run time is concerned. Regarding nested XML: probably feasable, but you will have to supply an nested XML as an example to work with! – Marcus Rickert Nov 02 '14 at 10:58