XPath query for empty namespace xmlns="" - xpath

What XPath do I use to query the info node in the xml below? I've tried different expressions in XMLSpy but nothing works.
<root xmlns="tempuri.org" xmlns:p="http://nonamespace.org/std/Name/2006-10-18/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<item xmlns="">
<info>blah blah</info>
<date>2009-07-27 00:00:00</date>
</item>

you can do it like this
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="tempuri.org">
<xsl:template match="/">
<xsl:value-of select="a:root/item/info"/>
</xsl:template>

Related

Is there a way to conditionally sort in xslt?

I need to sort some content but only when an attribute is equal to CAT. I think I should be able to pass a property from my ant build file to the use-when attribute but it is not working. Any help would be appreciated
Here is the xslt that I have:
<xsl:for-each select="document(#fileRef)/foo/bar">
<xsl:sort select="translate(child::Title/text(), '>', '')" order="ascending" use-when="system-property('customerCode')='CAT'"
collation="http://www.w3.org/2005/xpath-functions/collation/html-ascii-case-insensitive"/>
<!-- do some stuff here -->
</xsl:for-each>
Using oXygen I got the following to work in an Ant file:
<xslt in="sample1.xml" out="sample1-transformed.xml" force="true" style="sheet1.xsl">
<factory name="net.sf.saxon.TransformerFactoryImpl"/>
<sysproperty key="cat" value="bar"/>
</xslt>
XML sample1.xml is e.g.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>c</item>
<item>a</item>
</root>
XSLT is
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:comment select="system-property('cat')"/>
<xsl:next-match/>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates select="item">
<xsl:sort select="." use-when="system-property('cat') = 'foo'"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
and then the output items are sorted only if the Ant sets e.g. <sysproperty key="cat" value="foo"/>.

XPath Select Parent and Multiple Children

In Xpath how would I get my results to select all documents and display them like
<Document>
<Size>100</Size>
<Offset>200</Offset>
<FileName>x.doc</FileName>
</Document>
from
<Documents>
<Document>
<Size>100</Size>
<Offset>200</Offset>
<FileName>x.doc</FileName>
<Details>
<Header>asdfasdf</Header>
<Footer>adfasdfas</Footer>
<Author>asdfasdfs</Author>
<Date>20140101</Date>
</Details>
<Document>
</Documents>
Thanks.
With XSLT you get the result with
<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:template match="//Documents//*">
<xsl:if test="local-name(parent::*)!='Details' and local-name(.) != 'Details'">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
So you would get a copy of all Documents excluding the Details.

How to add an image title in XSL?

I would like to add a title below each image. My xsl-file looks like this where I loop through each image:
<Testresult>
<xsl:for-each select="Testresult/image">
<img src="{#href}" width="333px" length="350px"/>
<title><xsl:value-of select="//Type"/></title>
<xsl:attribute name="src">
<xsl:value-of select="Testresult/Image"/>
</xsl:attribute>
</xsl:for-each>
</Testresult>
My xml-file looks like this:
<Testresult>
<image href="c:\image1.png"/>
<image href="c:\image2.png"/>
</Testresult>
The output should look like this:
thanks for the help!
try this 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"/>
<xsl:template match="/">
<Testresult>
<xsl:for-each select="Testresult/image">
<img src="{#href}" width="333px" length="350px"/>
<title><xsl:value-of select="#href"/></title>
</xsl:for-each>
</Testresult>
</xsl:template>
</xsl:stylesheet>

Xpath Query Help. Selecting data from multiple paths as a single data set

I have an xml structure like the following :
<doc>
<line void="false">
<lineNumber>1</lineNumber>
<info1>ddddd</info1>
<info2>aaaaa</info2>
</line>
<line void="true">
<lineNumber>2</lineNumber>
<voidLineNumber>1</voidLineNumber>
<voidValue>2.00</voidValue>
</line>
</doc>
I need one single set of data. I would like to select all the lines where void = false as well as the voidLineNumber and voidValue data from the line where void = true and the voidLineNumber = lineNumber from the original line.
Is this possible? Any help would be appreciated. Thanks
As Michael Kay noted, XPath itself can only be used to select nodes, not to transform them. You can do what you want with XSLT:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" indent="yes" />
<xsl:template match="doc">
<xsl:apply-templates select="line" />
</xsl:template>
<xsl:template match="line">
<xsl:variable name="voidvalue"><xsl:value-of select="#void" /></xsl:variable>
<xsl:if test="$voidvalue='false'">
<xsl:copy>
<xsl:copy-of select="#*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:if test="name(.)='lineNumber'">
<xsl:value-of select="."/>
</xsl:if>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Spring Integration xslt-transformer doesn't work with namespace

I use Spring Integration:
<int-xml:xslt-transformer input-channel="partnerTransformation"
output-channel="serviceChannel"
xsl-resource="classpath:/META-INF/vendorTransformerXml.xsl"/>
The input XML message:
<ns1:persons xmlns:ns1="http://com.test.xslt/test" xmlns:ns3="http://universal.consumerrequest.schema.model.tci.ca">
<ns3:person>
<ns3:name>John</ns3:name>
<ns3:family>Smith</ns3:family>
</ns3:person>
<ns3:person>
<ns3:name>Raza</ns3:name>
<ns3:family>Abbas</ns3:family>
</ns3:person>
and the XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://com.test.xslt/test" xmlns:ns3="http://universal.consumerrequest.schema.model.tci.ca">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="ns1:persons/ns3:person">
<xsl:value-of select="ns3:family"/>
<xsl:value-of select="ns3:name"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
it doesn't work, but it works well without namesapce, appreciate if you can help.
The problem is the way Java XML parsers handle namespaces by default. There's a few things you can do:
Convert the input XML to a String before feeding it to the XSLT transformer. This works fine in my testing
If you are building the document yourself, e.g.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().parse(...);
If you don't have to worry about namespaces, probably the easiest thing is to change your XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://com.test.xslt/test"
xmlns:ns3="http://universal.consumerrequest.schema.model.tci.ca">
<xsl:output method="text" />
<xsl:template match="/">
<xsl:for-each select="//*[local-name()='person']">
<xsl:value-of select="*[local-name()='family']" />
<xsl:value-of select="*[local-name()='name']" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Resources