Here is a snip-it of the XML:
<?xml version="1.0" encoding="iso-8859-1" ?>
<NetworkAppliance id="S123456">
<Group id="9">
<Probe id="1">
<Value>74.7</Value>
</Probe>
</NetworkAppliance>
I want to get the single point value of 74.7. There are many groups with unique ID's and many Probes under that group with unique ID's each with values.
I am looking for example XSLT code that can get me this one value. Here is what i have that does not work:
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" version="3.2" />
<xsl:template match="NetworkAppliance">
<xsl:apply-templates select="Group[#id='9']"/>
</xsl:template>
<xsl:template match="Group">
Temp: <xsl:value-of select="Probe[#id='1']/Value"/>
<br/>
</xsl:template>
</xsl:stylesheet>
Here is what worked for me in the end:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="NetworkAppliance/Group[#id=9]/Probe[#id=1]">
Value: <xsl:value-of select="Value" />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Don't forget that you can do select several levels at once. Fixing your XML to:
<?xml version="1.0" encoding="iso-8859-1" ?>
<NetworkAppliance id="S123456">
<Group id="9">
<Probe id="1">
<Value>74.7</Value>
</Probe>
</Group>
</NetworkAppliance>
and using this stylesheet:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" version="3.2" />
<xsl:template match="/">
Temp: <xsl:value-of select="//Group[#id='9']/Probe[#id='1']/Value"/>
<br/>
</xsl:template>
</xsl:stylesheet>
we can pick out that one item you're interested in.
Points to note:
The // part of the expression means that the search for Group nodes takes place throughout the whole tree, finding Group nodes at whatever depth they're at.
The [#id='9'] part selects those Group nodes with id of 9
The Probe[#id='1'] part immediately after that selects those children of the Group nodes it found where the id is 1, and so on.
<xsl:value-of select="/NetworkAppliance/Group[#id=9]/Probe[#id=1]/Value"/>
XSLT is just one of the tools in the box, and nothing without XPath.
the xpath for value of a node is /node/text()
So
<xsl:value-of select="Probe[#id='1']/text()"/>
Related
I have a problem on inserting XML strings into the XSLT I have. Particularly, I have a sample XML string here:
<md:People>
<md:Job>
<md:JobFunction>Actor</md:JobFunction>
<md:BillingBlockOrder>1</md:BillingBlockOrder>
</md:Job>
<md:Name>
<md:DisplayName language="en-US">Vice Ganda</md:DisplayName>
</md:Name>
</md:People>
and I want to insert it into the XSLT I have (see <!-- INSERT "People" Metadata XML STRING HERE -->):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soa="urn:telestream.net:soa:core" exclude-result-prefixes='soa' version="1.0">
<xsl:variable name="basicContentID"><xsl:value-of select="/soa:Label/soa:Parameter[#name='basicContentID']/text()"/></xsl:variable>
<xsl:variable name="movieTitle"><xsl:value-of select="/soa:Label/soa:Parameter[#name='movieTitle']/text()"/></xsl:variable>
<xsl:variable name="releaseYear"><xsl:value-of select="/soa:Label/soa:Parameter[#name='releaseYear']/text()"/></xsl:variable>
<xsl:variable name="releaseDate"><xsl:value-of select="/soa:Label/soa:Parameter[#name='releaseDate']/text()"/></xsl:variable>
<xsl:template match="/">
<mdmec:CoreMetadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:md="http://www.movielabs.com/schema/md/v2.6/md" xmlns:mdmec="http://www.movielabs.com/schema/mdmec/v2.6" xsi:schemaLocation="http://www.movielabs.com/schema/mdmec/v2.6/mdmec-v2.6.xsd">
<mdmec:Basic ContentID="{/soa:Label/soa:Parameter[#name='basicContentID']/text()}">
<md:LocalizedInfo language="{/soa:Label/soa:Parameter[#name='metadataLanguage']/text()}">
<md:TitleDisplayUnlimited><xsl:value-of select="$movieTitle"/></md:TitleDisplayUnlimited>
</md:LocalizedInfo>
<!-- INSERT "People" Metadata XML STRING HERE -->
</mdmec:Basic>
<md:ReleaseYear><xsl:value-of select="$releaseYear"/></md:ReleaseYear>
<md:ReleaseDate><xsl:value-of select="$releaseDate"/></md:ReleaseDate>
</mdmec:CoreMetadata>
</xsl:template>
</xsl:stylesheet>
...to have an XML output like this:
<?xml version="1.0" encoding="utf-8"?>
<mdmec:CoreMetadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:md="http://www.movielabs.com/schema/md/v2.6/md" xmlns:mdmec="http://www.movielabs.com/schema/mdmec/v2.6" xsi:schemaLocation="http://www.movielabs.com/schema/mdmec/v2.6/mdmec-v2.6.xsd">
<mdmec:Basic ContentID="md:cid:org:abs_cbn:StarCinema-BeautyAndTheBestie2015">
<md:LocalizedInfo language="en-US">
<md:TitleDisplayUnlimited>Beauty and The Bestie</md:TitleDisplayUnlimited>
</md:LocalizedInfo>
<!-- Where "People" Metadata should be appearing -->
<md:People>
<md:Job>
<md:JobFunction>Actor</md:JobFunction>
<md:BillingBlockOrder>1</md:BillingBlockOrder>
</md:Job>
<md:Name>
<md:DisplayName language="en-US">Vice Ganda</md:DisplayName>
</md:Name>
</md:People>
</mdmec:Basic>
<md:ReleaseYear><xsl:value-of select="$releaseYear"/></md:ReleaseYear>
<md:ReleaseDate><xsl:value-of select="$releaseDate"/></md:ReleaseDate>
</mdmec:CoreMetadata>
Basically, the XML string I wanted to insert is in between the <mdmec:basic> code, and defining an xslt template in-between the root is not allowed. How can I go through this?
Thanks for all your help in advance!
EDIT: I tried to reproduce the sample from this thread [https://stackoverflow.com/questions/54535142/xml-string-to-xml-by-xslt] by re-creating the XML string I have:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
<md:People>
<md:Job>
<md:JobFunction>Actor</md:JobFunction>
<md:BillingBlockOrder>1</md:BillingBlockOrder>
</md:Job>
<md:Name>
<md:DisplayName language="en-US">Vice Ganda</md:DisplayName>
</md:Name>
</md:People>
</Root>
...and inserted into the XSLT I have:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soa="urn:telestream.net:soa:core" exclude-result-prefixes='soa' version="1.0">
<xsl:variable name="basicContentID"><xsl:value-of select="/soa:Label/soa:Parameter[#name='basicContentID']/text()"/></xsl:variable>
<xsl:variable name="movieTitle"><xsl:value-of select="/soa:Label/soa:Parameter[#name='movieTitle']/text()"/></xsl:variable>
<xsl:variable name="releaseYear"><xsl:value-of select="/soa:Label/soa:Parameter[#name='releaseYear']/text()"/></xsl:variable>
<xsl:variable name="releaseDate"><xsl:value-of select="/soa:Label/soa:Parameter[#name='releaseDate']/text()"/></xsl:variable>
<xsl:output omit-xml-declaration="yes" />
<xsl:template match="/">
<mdmec:CoreMetadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:md="http://www.movielabs.com/schema/md/v2.6/md" xmlns:mdmec="http://www.movielabs.com/schema/mdmec/v2.6" xsi:schemaLocation="http://www.movielabs.com/schema/mdmec/v2.6/mdmec-v2.6.xsd">
<mdmec:Basic ContentID="{/soa:Label/soa:Parameter[#name='basicContentID']/text()}">
<md:LocalizedInfo language="{/soa:Label/soa:Parameter[#name='metadataLanguage']/text()}">
<md:TitleDisplayUnlimited><xsl:value-of select="$movieTitle"/></md:TitleDisplayUnlimited>
</md:LocalizedInfo>
<xsl:template match="/Root">
<xsl:value-of select="normalize-space(.)" disable-output-escaping="yes" />
</xsl:template>
</mdmec:Basic>
<md:ReleaseYear><xsl:value-of select="$releaseYear"/></md:ReleaseYear>
<md:ReleaseDate><xsl:value-of select="$releaseDate"/></md:ReleaseDate>
</mdmec:CoreMetadata>
</xsl:template>
</xsl:stylesheet>
It appears to have an error: 'xsl:template' cannot be a child of the 'mdmec:Basic' element
If you manage to pass the XML string into your XSL transformation as a parameter, you can output it with escaping disabled:
<xsl:stylesheet ...>
<xsl:param name="xmlstring"/>
...
</md:LocalizedInfo>
<!-- INSERT "People" Metadata XML STRING HERE -->
<xsl:value-of select="$xmlstring" disable-output-escaping="yes"/>
</mdmec:Basic>
...
I am using a "xsl:for-each" to iterate over each element with name content and attribute that contains the text "period". When attempting to take out one date per each "xsl:for-each" iteration, it returns 2 values.
The matching of text "period" must be done like this due to the input data might change and it is unknown how many elements with id containing ="period", that would appear in the data.
I would like to keep the xpath search critera in the "xsl:for-each" syntax, because I am using the template to point out root.
When I try to subset the dates using date[1] it still return both dates.
XSLT Fiddle
Same code as in above fiddle:
Data:
<?xml version="1.0" encoding="utf-8" ?>
<section>
<content id="period1">
<date>2021-01-01</date>
</content>
<content id="period2">
<date>2020-01-01</date>
</content>
</section>
XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="#all"
>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="html" indent="yes" html-version="5"/>
<xsl:template match="/section">
<xsl:for-each select="//content/#*[contains(., 'period')]">
<date>
<!--<xsl:value-of select="."/>-->
<!--<xsl:value-of select="//date[1]"/>-->
<xsl:value-of select="//content/date"/>
</date>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Result:
<!DOCTYPE HTML>
<date>2021-01-01 2020-01-01</date>
<date>2021-01-01 2020-01-01</date>
Wanted result:
<!DOCTYPE HTML>
<date>2021-01-01</date>
<date>2020-01-01</date>
Use relative paths
<xsl:for-each select="content[#*[contains(., 'period')]]">
<date>
<!--<xsl:value-of select="."/>-->
<!--<xsl:value-of select="//date[1]"/>-->
<xsl:value-of select="date"/>
</date>
</xsl:for-each>
i have the following xml and xslt to render it, but got no results. I checked again and again and see not path problem, and the xsl went through the compiler. so I am not sure if it's namespace problem or something else. many thx!
XML file
<?xml version="1.0" encoding="UTF-8"?>
<bibdataset xsi:schemaLocation="http://www.elsevier.com/xml/ani/ani http://www.elsevier.com/xml/ani/embase_com.xsd"
xmlns="http://www.elsevier.com/xml/ani/ani"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ce="http://www.elsevier.com/xml/ani/common"
xmlns:ait="http://www.elsevier.com/xml/ani/ait">
<item>
<bibrecord>
<item-info>
<itemidlist><ce:doi>10.1258/0268355042555000</ce:doi>
</itemidlist>
</item-info>
<head>
<citation-title>
<titletext xml:lang="en" original="y">Effect of seasonal variations on the emergence of deep venous thrombosis of the lower extremity
</titletext>
</citation-title>
<abstracts>
<abstract xml:lang="en" original="y">
<ce:para>Objective: We aimed to determine the role of seasonal and meteorological variations in the incidence of lower extremity
</ce:para>
</abstract>
</abstracts>
</head>
</bibrecord>
</item>
</bibdataset>
XSLT file
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.elsevier.com/xml/ani/ani"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ce="http://www.elsevier.com/xml/ani/common"
xmlns:ait="http://www.elsevier.com/xml/ani/ait">
<xsl:output indent="yes" omit-xml-declaration="no"
media-type="application/xml" encoding="UTF-8" />
<xsl:template match="/">
<searchresult>
<xsl:apply-templates
select="/bibdataset/item/bibrecord" />
</searchresult>
</xsl:template>
<xsl:template match="bibrecord">
<document>
<title><xsl:value-of select="head/citation-title/titletext" /></title>
<snippet>
<xsl:value-of select="head/abstracts/abstract/ce:para" />
</snippet>
<url>
<xsl:variable name="doilink" select="item-info/itemidlist/ce:doi"/>
<xsl:value-of
select="concat('http://dx.doi.org/', $doilink)" />
</url>
</document>
</xsl:template>
</xsl:stylesheet>
This is indeed an issue with namespaces. In your XML, you have declared a default namespace meaning the root element, and all its descendants, and in this namespace.
<bibdataset .xmlns="http://www.elsevier.com/xml/ani/ani" ...
Now, in your XSLT, you have also declared this namespace, but without a prefix.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.elsevier.com/xml/ani/ani"
This means it only applies to the elements you are outputting, so your searchresult element gets output in this namespace
<searchresult xmlns="http://www.elsevier.com/xml/ani/ani"
However, it doesn't apply to the xpath expression in your XSLT, and so these are looking for elements in your input XML with no namespace.
In XSLT 2.0 the solution would be to simply declare an "xpath-default-namespace"
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.elsevier.com/xml/ani/ani"
xpath-default-namespace="http://www.elsevier.com/xml/ani/ani" ...
In XSLT 1.0, you will have to declare the namespace with a prefix, and use this prefix in all the xpath expressions.
Try this XSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.elsevier.com/xml/ani/ani"
xmlns:ani="http://www.elsevier.com/xml/ani/ani"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ce="http://www.elsevier.com/xml/ani/common"
xmlns:ait="http://www.elsevier.com/xml/ani/ait"
exclude-result-prefixes="ani">
<xsl:output indent="yes" omit-xml-declaration="no"
media-type="application/xml" encoding="UTF-8" />
<xsl:template match="/">
<searchresult>
<xsl:apply-templates
select="/ani:bibdataset/ani:item/ani:bibrecord" />
</searchresult>
</xsl:template>
<xsl:template match="ani:bibrecord">
<document>
<title><xsl:value-of select="ani:head/ani:citation-title/ani:titletext" /></title>
<snippet>
<xsl:value-of select="ani:head/ani:abstracts/ani:abstract/ce:para" />
</snippet>
<url>
<xsl:variable name="doilink" select="ani:item-info/ani:itemidlist/ce:doi"/>
<xsl:value-of
select="concat('http://dx.doi.org/', $doilink)" />
</url>
</document>
</xsl:template>
</xsl:stylesheet>
Note that you can remove the line xmlns="http://www.elsevier.com/xml/ani/ani" but that would mean your searchresult (and other) elements would be output with no namespace, so you would need to output it as <ani:searchresult> if you wanted it in the given namespace.
My source XML looks:
<test>
<text1>Test</text1>
<text2>Test</text2>
<text2>Test</text2>
<section>
<text1>Test<bold>content</bold></text1>
<text1>Test</text1>
<text2>Test</text2>
<text2>Test</text2>
</section>
</test>
Want to extract the value of 6th node, based on the absolute number of the element (overall count). The absolute number of the element has been identified using <xsl:number level="any" from="/" count="*"/>.
The XPath expression /descendant::*[6] should give you the element you need.
<xsl:template match="/">
<xsl:copy-of select="/descendant::*[6]" />
</xsl:template>
outputs
<text1>Test<bold>content</bold></text1>
Note that this is an example of the difference between descendant:: and // - //*[6] would give you all elements that are the sixth child element of their respective parent, rather than simply the sixth element in the document in depth-first order.
This xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="allElements" select="//element()" />
<xsl:template match="/">
<output>
<xsl:value-of select="$allElements[6]" />
</output>
</xsl:template>
</xsl:stylesheet>
will result in
<?xml version="1.0" encoding="UTF-8"?>
<output>Testcontent</output>
I have the following xml:
<RootNode xmlns="http://someurl/path/path/path">
<Child1>
<GrandChild1>Value</GrandChild1>
<!-- Lots more elements in here-->
</Child1>
</RootNode>
I have the following xslt:
<xsl:stylesheet version="1.0" xmlns="http://someurl/path/path/path" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<NewRootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NewChild1>
<xsl:for-each select="RootNode/Child1">
<NewNodeNameHere>
<xsl:value-of select="GrandChild1"/>
</NewNodeNameHere>
<!-- lots of value-of tags in here -->
</xsl:for-each>
</NewChild1>
</NewRootNode >
</xsl:template>
</xsl:stylesheet>
The problem: this is the my result:
<?xml version="1.0" encoding="utf-8"?>
<NewRootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NewChild1 />
</NewRootNode>
I am expecting to see:
<?xml version="1.0" encoding="utf-8"?>
<NewRootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NewChild1>
<NewNodeNameHere>Value</NewNodeNameHere>
<!-- Other new elements with values from the xml file -->
</NewChild1>
</NewRootNode>
I am missing of the information inside of NewChild1 that should be there.
I think my for-each select is correct, so the only thing I can think of is that there is a problem with the namespace in the Xml and the namespace in the xslt. Can anybody see what I'm doing wrong?
The problem is caused by the namespaces.
Since the xml defines xmlns="http://someurl/path/path/path", it is not in the default namespace anymore.
You can define that namespace with an name like xmlns:ns="http://someurl/path/path/path" in the xsl, and then use that name in the XPath expression.
The following works for me:
<xsl:stylesheet version="1.0" xmlns:ns="http://someurl/path/path/path" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<NewRootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NewChild1>
<xsl:for-each select="ns:RootNode/ns:Child1">
<NewNodeNameHere>
<xsl:value-of select="ns:GrandChild1"/>
</NewNodeNameHere>
<!-- lots of value-of tags in here -->
</xsl:for-each>
</NewChild1>
</NewRootNode >
</xsl:template>
</xsl:stylesheet>
The stylesheet namespace should be http://www.w3.org/1999/XSL/Transform instead of http://someurl/path/path/path.
Also, since the input XML uses a namespace all your XPath expressions should be namespace-qualified:
<xsl:template match="/" xmlns:ns1="http://someurl/path/path/path">
<NewRootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<NewChild1>
<xsl:for-each select="ns1:RootNode/ns1:Child1">
<NewNodeNameHere>
<xsl:value-of select="ns1:GrandChild1"/>
</NewNodeNameHere>
<!-- lots of value-of tags in here -->
</xsl:for-each>
</NewChild1>
</NewRootNode>
</xsl:template>