Work with XML and XSL

FIRST EDIT
I take the Child 1 tag in DropDownList in C # form, Plz offers a code of best practice (C #) to remove the Parent tag and all its child tags in the XML file. Example:

<Parents> <Parent> <Child 1>Something</Child 1> <Child 2>Something</Child 2> <Child 3>Something</Child 3> <Child 4>Something</Child 4> </Parent> <Parent> <Child 1>Something 1</Child 1> <Child 2>Something 1</Child 2> <Child 3>Something 1</Child 3> <Child 4>Something 1</Child 4> </Parent> </Parents> 

--- Previous question ---
How can I insert the following style sheet tag into my new xml file that is generated using C # code?

 <?xml-stylesheet type="text/xsl" href="issuez.xsl"?> 

C # code to create an xml file: -

 new XDocument( new XElement("issues", new XElement("issue", new XElement("cat", comboBox1.Text), new XElement("desc", richTextBox1.Text), new XElement("end", dateTimePicker1.Text), new XElement("att", textBox2.Text) ) ) ).Save(path); 
+2
source share
4 answers

First, make sure that the dates in your XML are represented in the canonical format YYYY-MM-DD, and the times are HH: MM: SS, so that XSLT (which does not have date or time data types in 1.0) can compare and sort them.

Secondly, use the Steve Muench method to group. You generate a key by item dates using xsl:key . Then, the key() function can be found to find a list of all elements on a given date.

Using this key, you can create a list of individual dates displayed in items. This is the Muenchian method: find each element that the first element in the list returns key () for this position. This method ensures that you always get one and only one item for each individual date value. You then sort these items and use their dates to control the actual release of your product.

Minimal example:

 <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:key name="dates" match="/data/newsitem" use="@date"/> <xsl:template match="/"> <output> <!-- find exactly one newsitem node for each distinct @date in the document --> <xsl:for-each select="/data/newsitem[generate-id() = generate-id(key('dates', @date)[1])]"> <xsl:sort select="@date" order="descending"/> <xsl:call-template name="newsitems_for_date"> <xsl:with-param name="date" select="@date"/> </xsl:call-template> </xsl:for-each> </output> </xsl:template> <xsl:template name="newsitems_for_date"> <xsl:param name="date"/> <h1> <xsl:value-of select="$date"/> </h1> <xsl:apply-templates select="/data/newsitem[@date=$date]"> <xsl:sort select="@time" order="descending"/> </xsl:apply-templates> </xsl:template> <xsl:template match="newsitem"> <p> newsitem for <xsl:value-of select="@date"/> </p> </xsl:template> </xsl:stylesheet> 
+3
source

This is the thing that the back end that creates the XML should handle. XSLT is not the best place for logic. It’s better to embed it all in XML after a request for news. Just send them to the customer in proper form so that they do not work so hard.

+2
source

The XSLT stylesheet allows you to set global options before starting the conversion. So, with XSLT 1.0 and .NET XslCompiledTransform, if you need the current date in the stylesheet, you can define a global parameter

  <xsl:param name="current-date"/> 

and set this before starting the conversion by creating an XsltArgumentList, setting the parameter to the value and format you want / need , and then skip that XsltArgumentList is the second argument to the Transform method. Then in the stylesheet, you can compare the date in the XML input element or attribute with the parameter.

How do you use .NET, another option is to use XSLT 2.0; Microsoft does not support this, but with Saxon 9 there is a third-party solution. XSLT / XPath 2.0 have a function called current-date , so you don't need a parameter.

+1
source

If you use the XSLT processor from PHP, you can use the PHP functions inside your XSLT script. All you need to do is call registerPhpFunctions before converting. For sorting, you can use the result value in the correct order.

0
source

Source: https://habr.com/ru/post/1303151/


All Articles