Basically, I want an exact copy of the XML, except I'd like to sort certain nodes by their ID attribute. Not all elements with the ID attribute should be sorted.
I've kludged together a working stylesheet, but it requires me to hard-code the <xsl:apply-template>'s in for all sibling nodes to the nodes I'm sorting on. I've no formal schema, and I can't be certain that it won't change. I'd like to be able to create a more generic stylesheet which will output the non-sorted nodes without these explicit calls.
Is this possible?
My Java code to transform the XML:
String input = "C:\\presort.xml";
String output = "C:\\postsort.xml";
String xsl = "C:\\sort.xsl"
Document dom;
try
{
dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);
Element e = dom.getDocumentElement();
Transformer transform = TransformerFactory.newInstance().newTransformer(
new StreamSource( new File(xsl) ));
StreamResult result = new StreamResult(new File(output));
transform.transform(new DOMSource(dom), result);
}
catch (Exception e)
{
e.printStackTrace();
}
My XML:
<?xml version="1.0" encoding="utf-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Person id="1">
<Base>true</Base>
<Description>something</Description>
<Name>Vin Vinigar</Name>
<Privileges>
<Privilige id="340">
<Permission>2</Permission>
<Job id="dontsort" />
</Privilige>
<Privilige id="11">
<Permission>3</Permission>
<Job id="dontsort" />
</Privilige>
<Privilige id="1011">
<Permission>1</Permission>
<Job id="2342" />
</Privilige>
</Privileges>
</Person>
<Person id="f32">
<Base>true</Base>
<Description>Here be dragons</Description>
<Name>John Doe</Name>
<Privileges>
<Privilige id="23a">
<Permission>2</Permission>
<Job id="a2a" />
</Privilige>
</Privileges>
</Person>
<Person id="22">
<PossibleUnknownTagHere>something</PossibleUnknownTagHere>
<Name>Han Solo</Name>
<Privileges>
<Privilige id="23a">
<Permission>3</Permission>
<Job id="a2a" />
</Privilige>
</Privileges>
</Person>
</root>
My stylesheet:
<?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="root|#*">
<xsl:copy >
<xsl:apply-templates select="#*" />
<xsl:apply-templates select="Person">
<xsl:sort select="#id" order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="Person">
<xsl:copy >
<xsl:apply-templates select="#*" />
<!-- Works but requies a template for each sibling element -->
<xsl:apply-templates select="Base" />
<xsl:apply-templates select="Description" />
<xsl:apply-templates select="Name" />
<!-- I'd like to do something like this -->
<!--
<xsl:choose>
<xsl:when test="not(Privileges)">
<xsl:apply-templates select="." />
</xsl:when>
</xsl:choose>
-->
<xsl:apply-templates select="Privileges" />
</xsl:copy>
</xsl:template>
<!-- I'd like to remove the need for these 3 explicity copies -->
<xsl:template match="Base">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="Description">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="Name">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="Privileges">
<xsl:copy >
<xsl:apply-templates select="#*" />
<xsl:apply-templates select="Privilege">
<xsl:sort select="#id" order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="Privilege">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="node() | #*">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
<xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates select="#*" />
<xsl:apply-templates select="Person">
<xsl:sort select="#id" order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="Privileges">
<xsl:copy>
<xsl:apply-templates select="#*" />
<xsl:apply-templates select="Privilege">
<xsl:sort select="#id" order="ascending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
Related
My requirement is as follows
If any complex element contains only text then corresponding element attributes translate into element to parent level.
If any complex element contains children then corresponding element attributes translate into element to same element level.
This translation want to achieve using xslt logic.
Input XML
<root>
<food name="desert">butter scotch</food>
<special type="nonveg">
<name>chicken</name>
</special>
</root>
Output XML
<root>
<food>butter scotch</food>
<name>desert</name>
<special>
<type>nonveg</type>
<name>chicken</name>
</special>
</root>
Start off with the Identity Transform....
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
For the rule "If any complex element contains only text then corresponding element attributes translate into element to parent level", you could use the following template (I am ignoring comments and processing-instuctions here, and just checking the element has no child element)
<xsl:template match="*[not(*)][#*]">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
<xsl:apply-templates select="#*" mode="toelement"/>
</xsl:template>
The template with mode "toelement" will turn the attribute to an element. (It will be re-used by the other rule).
<xsl:template match="#*" mode="toelement">
<xsl:element name="{local-name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
For the rule "If any complex element contains children then corresponding element attributes translate into element to same element level." then you could actually match on the attribute directly:
<xsl:template match="*[*]/#*">
<xsl:apply-templates select="." mode="toelement"/>
</xsl:template>
Try this XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="*[*]/#*">
<xsl:apply-templates select="." mode="toelement"/>
</xsl:template>
<xsl:template match="*[not(*)][#*]">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
<xsl:apply-templates select="#*" mode="toelement"/>
</xsl:template>
<xsl:template match="#*" mode="toelement">
<xsl:element name="{local-name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I was trying to find a way to convert the XML tags to their respective unique address like xPath. Nd I found an XSLT, where XML is processed and the unique address is created only for the nodes which doesnt have child elements and with attributes. [link]:Generate/get xpath from XML node java
XSLT :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vApos">'</xsl:variable>
<xsl:template match="*[#* or not(*)] ">
<xsl:if test="not(*)">
<xsl:apply-templates select="ancestor-or-self::*" mode="path"/>
<xsl:value-of select="concat('=',$vApos,.,$vApos)"/>
<xsl:text>
</xsl:text>
</xsl:if>
<xsl:apply-templates select="#*|*"/>
</xsl:template>
<xsl:template match="*" mode="path">
<xsl:value-of select="concat('/',name())"/>
<xsl:variable name="vnumPrecSiblings" select=
"count(preceding-sibling::*[name()=name(current())])"/>
<xsl:if test="$vnumPrecSiblings">
<xsl:value-of select="concat('[', $vnumPrecSiblings +1, ']')"/>
</xsl:if>
</xsl:template>
<xsl:template match="#*">
<xsl:apply-templates select="../ancestor-or-self::*" mode="path"/>
<xsl:value-of select="concat('[#',name(), '=',$vApos,.,$vApos,']')"/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
when passed to this
<?xml version="1.0" encoding="UTF-8"?>
<root>
<main>
<tag1>001</tag1>
<tag2>002</tag2>
<tag3>
<tag4>004</tag4>
</tag3>
<tag2>002</tag2>
<tag5>005</tag5>
</main>
</root>
produces
/root/main/tag1='001' /root/main/tag2='002' /root/main/tag3/tag4='004' /root/main/tag2[2]='002' /root/main/tag5='005'
So I need the xslt to generate in the following way
/root
/root/main
/root/main/tag1
/root/main/tag2
/root/main/tag3
/root/main/tag3/tag4
/root/main/tag2[2]
/root/main/tag5
Also I dont need values. So please help me with this
Your result could be produced rather simply by:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="*">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="name()" />
<xsl:variable name="i" select="count(preceding-sibling::*[name()=name(current())])"/>
<xsl:if test="$i">
<xsl:value-of select="concat('[', $i + 1, ']')"/>
</xsl:if>
<xsl:if test="position()!=last()">
<xsl:text>/</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
<xsl:apply-templates select="*"/>
</xsl:template>
</xsl:stylesheet>
Note that this does not process attributes (or any other type of nodes other than elements).
I have a text file as given below:
value1 value2 value3 value4
I want to convert it as following xml using XSLT
<values>
<value>value1</value>
<value>value2</value>
<value>value3</value>
<value>value4</value>
</values>
Thanks in advance.
Assuming XSLT 2.0,
<xsl:template name="main">
<values>
<xsl:for-each select="tokenize(unparsed-text('input.txt'), '\s+')">
<value><xsl:value-of select="."/></value>
</xsl:for-each>
</values>
</xsl:template>
if you can edit your input such that it contains a root element, and a tab character as a separator, such as below:
<root>value1 value2 value3 value4</root>
then, you can apply the following stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<values>
<xsl:call-template name="tokenizeString">
<xsl:with-param name="list" select="."/>
<xsl:with-param name="delimiter" select="' '"/>
</xsl:call-template>
</values>
</xsl:template>
<xsl:template name="tokenizeString">
<!--passed template parameter -->
<xsl:param name="list"/>
<xsl:param name="delimiter"/>
<xsl:choose>
<xsl:when test="contains($list, $delimiter)">
<value>
<!-- get everything in front of the first delimiter -->
<xsl:value-of select="substring-before($list,$delimiter)"/>
</value>
<xsl:call-template name="tokenizeString">
<!-- store anything left in another variable -->
<xsl:with-param name="list" select="substring-after($list,$delimiter)"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="$list = ''">
<xsl:text/>
</xsl:when>
<xsl:otherwise>
<value>
<xsl:value-of select="$list"/>
</value>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
and produce:
<values>
<value>value1</value>
<value>value2</value>
<value>value3</value>
<value>value4</value>
</values>
Using http://xslt.online-toolz.com/tools/xslt-transformation.php
.xml
<?xml version="1.0"?>
<my:project xmlns:my="http://myns">
<my:properties>
<my:property>
<my:name>customerId</my:name>
<my:value>1</my:value>
</my:property>
<my:property>
<my:name>userId</my:name>
<my:value>20</my:value>
</my:property>
</my:properties>
</my:project>
I now want to look for the name customerId and want to replace the value.
It almost works, but it replaces ALL values in the document. What am I doing wrong to just replace the value where the name mached?
.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://myns" xmlns:saxon="http://saxon.sf.net">
<xsl:param name="name" select="'customerId'"/>
<xsl:param name="value" select="'0'"/>
<xsl:template match="node() | #*">
<xsl:copy>
<xsl:apply-templates select="node() | #*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*/my:properties/my:property/my:value/text()" >
<xsl:choose>
<xsl:when test="/*/my:properties/my:property/my:name = $name">
<xsl:value-of select="$value"/>
</xsl:when>
<xsl:otherwise><xsl:copy-of select="saxon:parse(.)" /></xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
The test for /*/my:properties/my:property/my:name = $name always succeed because it uses an absolute path, and so the result is independent of the surrounding template context. A test with a relative xpath should work.
XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://myns" xmlns:saxon="http://saxon.sf.net">
<xsl:param name="name" select="'customerId'"/>
<xsl:param name="value" select="'0'"/>
<xsl:template match="node() | #*">
<xsl:copy>
<xsl:apply-templates select="node() | #*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*/my:properties/my:property/my:value/text()" >
<xsl:choose>
<xsl:when test="../../my:name = $name">
<xsl:value-of select="$value"/>
</xsl:when>
<xsl:otherwise>otherwise</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
XML:
<my:project xmlns:my="http://myns">
<my:properties>
<my:property>
<my:name>customerId</my:name>
<my:value>1</my:value>
</my:property>
<my:property>
<my:name>userId</my:name>
<my:value>20</my:value>
</my:property>
</my:properties>
</my:project>
Result of saxonb-xslt -s:test.xml -xsl:test.xsl
<my:project xmlns:my="http://myns">
<my:properties>
<my:property>
<my:name>customerId</my:name>
<my:value>0</my:value>
</my:property>
<my:property>
<my:name>userId</my:name>
<my:value>otherwise</my:value>
</my:property>
</my:properties>
</my:project>
I am beginner in XSLT.
My Source XML is as below
<Options>
<Option>
<Data>Data1</Data>
<Type>A</Type>
</Option>
<Option>
<Data>Data2</Data>
<Type>B</Type>
</Option>
<Option>
<Data>Data3</Data>
<Type>C</Type>
</Option>
<Option>
<Data>Data4</Data>
<Type>D</Type>
</Option>
...
</Options>
I have parameter which is used to filter the result from above method and it is as below
<xsl:param name="filterType" select="'A,C'"/>
The output should be as below:
<Result>
<Data Type="A">Data1<Data>
<Data Type="C">Data3<Data>
</Result>
Below is the XSLT i have created:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="filterType" select="'A,C'"/>
<xsl:template match="Options">
<xsl:element name="Result">
<xsl:apply-templates select="Option"/>
</xsl:element>
</xsl:template?
<xsl:template match="Option">
<xsl:element name="Data">
<xsl:attribute name="Type">
<xsl:value-of select="Type"/>
</xsl:attribute>
<xsl:value-of select="Data"/>
</xsl:element>
</xsl:template?
</xsl:stylesheet>
While applying template for 'Option' tag i need to use filterType.
How can i do that? Please Help.
I think you simply want
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="filterType" select="'A,C'"/>
<xsl:variable name="filter" select="concat(',', $filterType, ',')"/>
<xsl:template match="Options">
<Result>
<xsl:apply-templates select="Option[contains($filter, concat(',', Type, ','))]"/>
</Result>
</xsl:template>
<xsl:template match="Option">
<Data Type="{Type}">
<xsl:value-of select="Data"/>
</Data>
</xsl:template>
</xsl:stylesheet>