473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how do i create subset and union in XPath Query

Hi Friends,

I need a XPath query, whcih ll return me subset of data....
check below xml stream

<label id="MyExpenseDetails_lbl" xlink:role="terseLabel">Short Expense
Details</label>
<label id="MyExpenseDetails_lbl" xlink:role="displayLabel">Expense
Details</label>
<label id="InternalExpense_lbl" xlink:role="displayLabel">Internal
Expense</label>
<label id="ExternalExpense_lbl" xlink:role="terseLabel">Short External
Expense</label>

See above xml data having 4 label elements out of which 3 different IDs
and 2 different role.
MyExpenseDetails_lbl contain all 2 role, InternalExpense_lbl having
displayLabel and ExternalExpense_lbl having terseLabel

Now i want such XPath query or XQL command that return me collection of
label.
if user pass "terseLabel" label then label with role = "terseLabel"
should be return and the other one "InternalExpense_lbl" should also
return.

If user pass "displayLabel" label then label with only role =
"displayLabel" should be return.

Cond1: role = "terseLabel"
MyExpenseDetails_lbl (Short Expense Details) (only one record with role
= terseLabel )
InternalExpense_lbl (Internal Expense)
ExternalExpense_lbl (Short External Expense)

Cond2: role = "displayLabel"
MyExpenseDetails_lbl (Expense Details) (only one record with role =
displayLabel )
InternalExpense_lbl (Internal Expense)

There r also other more label roles , so i need it using XPath / XQL
query not by any hard coding

I tried below XPath Query but it's returning all 4 labels and i only
need as per my requirements. I mean if other label not exist for any
label then the default one i.e displayLabel should taken.

default:label[@xml:lang and @xlink:role=("terseLabel" or
"displayLabel")]

Thanks in advance
Regards
Rushikesh

Oct 26 '05 #1
3 2618
I guess the big question is:

how is the user "passing" the required parameters ?

via a VBScript or JScript ?

via XSLT ?

I'm not familiar with XQL, but there are a few ways to pass parameters
and have them used in XPath, depending on the method you choose.

Oct 27 '05 #2
Hi,

I m passing it from ASP.NET code (C# application)...but it doesn't
matter....I am only interested in XPath Query...
Bcoz we have one class XMLDocument in which i have to pass a valid
XPath Query and that function ll return me set of XMLNodes...

currently i m passing below xpath string and that function is returning
me all 4 records which i don't required....
default:label[@xml:lang and @xlink:role=("terseLabel" or
"displayLabel")]

Thanks & Regards
Rushikesh

Oct 28 '05 #3
This is from the MS XML 4.0 SDK.

You can pass a parameter to an XSL file and have the XSL retrieve the
nodes you
want.

This way you can supply the XML, the XSL and the parameter and it'll
return the
HTML (or output) which you can then response.write to the page.

Example

var xslt = new ActiveXObject("Msxml2.XSLTemplate.4.0");
var xslDoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.4.0" );
var xslProc;
xslDoc.async = false;
xslDoc.resolveExternals = false;
xslDoc.load("sample.xsl");
xslt.stylesheet = xslDoc;
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
xmlDoc.async = false;
xmlDoc.resolveExternals = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode <> 0) {
var myErr = xmlDoc.parseError;
alert("You have error " + myErr.reason);
} else {
xslProc = xslt.createProcessor();
xslProc.input = xmlDoc;
xslProc.addParameter("param1", "Hello");
xslProc.transform();
alert(xslProc.output);
}
File Name: Sample.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:param name="param1"/>
<xsl:template match="/">
The parameter value was: <xsl:value-of select="$param1"/>
</xsl:template>
</xsl:stylesheet>
[Visual Basic]
Visual Basic Syntax
objXSLProcessor.addParameter(baseName, parameter, namespaceURI)
Parameters
baseName
The name that will be used inside the style sheet to identify the
parameter context.
parameter
A number, Boolean, string, IXMLDOMNodeList, or IXMLDOMNode. Passing in
a single node will produce a node list that contains one node
(shortcut). To remove a parameter previously added to the processor,
provide a value of Empty or Null instead. This acts as a signal to the
processor to remove any previously added parameter of the same name.
namespaceURI (optional)
An optional namespace.
Example
Dim xslt As New Msxml2.XSLTemplate40
Dim xslDoc As New Msxml2.FreeThreadedDOMDocument40
Dim xmlDoc As New Msxml2.DOMDocument40
Dim xslProc As IXSLProcessor
xslDoc.async = False
xslDoc.resolveExternals = False
xslDoc.Load "sample.xsl"
Set xslt.stylesheet = xslDoc
xmlDoc.async = False
xmlDoc.resolveExternals = False
xmlDoc.Load "books.xml"
If (xmlDoc.parseError.errorCode <> 0) Then
Dim myErr
Set myErr = xmlDoc.parseError
MsgBox("You have error " & myErr.reason)
Else
Set xslProc = xslt.createProcessor()
xslProc.input = xmlDoc
xslProc.addParameter "param1", "Hello"
xslProc.Transform
MsgBox xslProc.output
End If
File Name: Sample.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:param name="param1"/>
<xsl:template match="/">
The parameter value was: <xsl:value-of select="$param1"/>
</xsl:template>
</xsl:stylesheet>
[C/C++]
C/C++ Syntax
HRESULT addParameter (BSTR baseName, VARIANT parameter, BSTR
namespaceURI);
Parameters
baseName [in]
The name that will be used inside the style sheet to identify the
parameter context.
parameter [in]
A number, Boolean, string, node list, or node. Passing in a single node
will produce a node list that contains one node (shortcut). To remove a
parameter previously added to the processor, you can pass a value of
VT_EMPTY, VT_NULL, or a NULL IDispatch or IUnknown instead. This acts
as a signal to the processor to remove any previously added parameter
of the same name.
namespaceURI [in, optional]
An optional namespace.
C/C++ Return Values
E_FAIL if readyState is READYSTATE_INTERACTIVE.

Example
#include "stdio.h"

#import <msxml4.dll>
using namespace MSXML2;

int checkParseError(IXMLDOMParseErrorPtr pError);
void dump_com_error(_com_error &e);
int main(int argc, char* argv[])
{

CoInitialize(NULL);
HRESULT hr;

try{

BOOL bResult = FALSE;
short sResult = FALSE;
IXMLDOMDocument2Ptr pStyleSheet=NULL;
IXSLTemplatePtr pIXSLTemplate=NULL;
IXSLProcessorPtr pXSLProcessor=NULL;

hr = pIXSLTemplate.CreateInstance(__uuidof(XSLTemplate4 0));
hr=pStyleSheet.CreateInstance(__uuidof(FreeThreade dDOMDocument40));
pStyleSheet->async = VARIANT_FALSE;
pStyleSheet->resolveExternals = VARIANT_FALSE;
hr=pStyleSheet->load("c:\\samplexsl.xml");
//check on the parser error
if(hr!=VARIANT_TRUE)
{
return checkParseError(pStyleSheet->parseError);
}

pIXSLTemplate->stylesheet = pStyleSheet.GetInterfacePtr();
pXSLProcessor = pIXSLTemplate->createProcessor();

IXMLDOMDocumentPtr pInputDoc;

hr = pInputDoc.CreateInstance(__uuidof(DOMDocument40));
pInputDoc->async = VARIANT_FALSE;
pInputDoc->resolveExternals = FALSE;
hr = pInputDoc->load("c:\\sampleXSLWithParam.xml");
//check on the parser error
if(hr!=VARIANT_TRUE)
{
return checkParseError(pInputDoc->parseError);
}

pInputDoc->async = VARIANT_FALSE;
pInputDoc->resolveExternals = FALSE;
pXSLProcessor->input = pInputDoc.GetInterfacePtr();

hr=pXSLProcessor->addParameter("param1", "Hello", "");

VARIANT_BOOL vtRet = pXSLProcessor->transform();
if(vtRet != VARIANT_TRUE)
{
MessageBox(NULL, "transformation failed","Error", MB_OK);
return -1;
}
_bstr_t bstrOutput = pXSLProcessor->Getoutput();
MessageBox(NULL, bstrOutput,"Transformed Output", MB_OK);

}
catch(_com_error &e)
{
dump_com_error(e);
}
return 0;
}
int checkParseError(IXMLDOMParseErrorPtr pError)
{
_bstr_t parseError =_bstr_t("At line ")+ _bstr_t(pError->Getline())
+ _bstr_t("\n")+ _bstr_t(pError->Getreason());
MessageBox(NULL,parseError, "Parse Error",MB_OK);
return -1;

}

void dump_com_error(_com_error &e)
{
printf("Error\n");
printf("\a\tCode = %08lx\n", e.Error());
printf("\a\tCode meaning = %s", e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
printf("\a\tSource = %s\n", (LPCSTR) bstrSource);
printf("\a\tDescription = %s\n", (LPCSTR) bstrDescription);
}
Style Sheet: "d:\\inetpub\\wwwroot\\sampleXSLWithParam.xml"

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:param name="param1"/>
<xsl:template match="/">
The parameter value was: <xsl:value-of select="$param1"/>
</xsl:template>
</xsl:stylesheet>
Output (in a message box)

<?xml version="1.0" encoding="UTF-16"?>
<bar>
Add Parameter Test
</bar>

Oct 28 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Terry P | last post by:
Are there any tools (java classes, tag libraries) which can translate xpath statements into a SQL query? Given an xpath query which has a predicate that filters node values or attributes, I want...
3
by: Paradigm | last post by:
I am using Access 2K as a front end to a MYSQL database. I am trying to run a Union query on the MYSQL database. The query is (much simplified) SELECT as ID from faxdata UNION SELECT as ID ...
7
by: Ot | last post by:
I posted this to the wrong group. It went to m.p.dotnet.languages.vb. Ooops. -------------------------------------------------------------------- I have this tiny problem. I have learned...
3
by: IMS.Rushikesh | last post by:
Hi Friends, I need a XPath query, whcih ll return me subset of data.... check below xml stream <label id="MyExpenseDetails_lbl" xlink:role="terseLabel">Short Expense Details</label> <label...
36
by: Robert Vazan | last post by:
I am looking for other people's attempts to create safe subset of C and enforce it with scripts. Does anybody know about anything like this? By "safe", I mean the following: * Strongly typed...
10
by: Michael C# | last post by:
OK, here's the deal. I have a small XML file that represents a small database table. I load it into a System.XML.XMLDocument. So far so good. I run an XPath query against it to retrieve all the...
5
by: Gnic | last post by:
Hi , I have an XmlDocument instance, I want to find a node in the xml, but I don't know it's path until runtime, for example <aaa> <bbb name="x"/> <aaa attr="y"> <ccc>sometext</ccc> </aaa>
2
by: MLH | last post by:
Fields in MyTable: PostID PostDate RollQtyXfer RollDenomination RollCount37 RollCount23
6
by: Steve | last post by:
I have a 2 unmatched queries Table1_view (records in table1 not in table2) Table2_view (records in table2 not in table1) Both table contain fields ReportID ReportName ReportOwner In_Table1...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.