Copy link to clipboard
Copied
Hi All,
EvaluateXPathsForElement does not giving the xml elements in the right order as in the xml structure.
Is there anyother way to get xml elements as in the xml structure order using xpath?
Regards,
Chinna
Copy link to clipboard
Copied
Any help?
Copy link to clipboard
Copied
Any help plz?
Copy link to clipboard
Copied
Haven't used them from the plugin side, but XML rules could be an alternative. At least with CS4 scripting there were subtle differences between the older (CS3) XML rules and the later (CS4) addition of EvaluateXPaths.
For a quick scripted test, see my old simpleXMLRule.jsx script.
Copy link to clipboard
Copied
Hi Dirk,
Sorry for the late reply. And many thanks for your reply.
Is it possible to return xml elements from script using IScriptRunner? I'm trying with the below code but I'm unable to get the result.
| WideString path("F:/script/Test.jsx"); |
IDFile scriptFile(path);
| InterfacePtr<IScriptManager> scriptManager(Utils<IScriptUtils>()->QueryScriptManager(kJavaScriptMgrBoss)); | |
| InterfacePtr<IScriptEngine> scriptEngine(scriptManager->QueryDefaultEngine()); | |
| InterfacePtr<IScriptRunner>scriptRunner(Utils<IScriptUtils>()->QueryScriptRunner(scriptFile)); |
| PMString theReturnString; | |
| PMString theErrorString; |
| ScriptRecordData args; | |
| ScriptData sScript(scriptFile); | |
| ScriptData sResultData; | |
| Utils<IScriptArgs>()->Save(); | |
| Utils<IScriptArgs>()->Set("xpath","//qlink[@ref-type]"); | |
| ErrorCode status = Utils<IScriptUtils>()->DispatchScriptRunner(scriptRunner, sScript, args, sResultData, theErrorString, kFalse); |
Regards,
Chinna
Copy link to clipboard
Copied
I did not mean to use a script for production code, just try your trouble xpath expression and test document to see whether the xml rule approach makes any difference at all. Only if it looks promising we should then try to get the rules processor running from the plugin side, not call it thru scripting.
Copy link to clipboard
Copied
Hi Dirk,
Thanks for your reply.
Yes xml rule makes difference. It returns the xml elements as in the xml structure from top to bottom. But evaluateXPathExpression returns the xml elements as last-in last order.
Here is the code I'm trying now. It returns the last inserted elements in the structure as first and so on.
IDocument *doc = Utils<ILayoutUIUtils>()->GetFrontDocument();
UIDRef docref = ::GetUIDRef(doc);
IDataBase *db = docref.GetDataBase();
K2Vector<WideString> xPaths;
xPaths.push_back(WideString("//qlink[@ref-type]"));
InterfacePtr<IIDXMLElement> rootElement(Utils<IXMLUtils>()->QueryRootElement(db));
XMLReference root(Utils<IXMLUtils>()->GetImportedRootXMLReference(doc));
HitMatches matches;
Utils<IXMLParserUtils>()->EvaluateXPathsForElement(xPaths, rootElement, matches, 0);
std::map<XMLReference, K2Vector<int32>>::reverse_iterator iterator = matches.rbegin();
std::map<XMLReference, K2Vector<int32>>::reverse_iterator iterator_end = matches.rend();
while(iterator != iterator_end)
{
WideString wstr;
XMLReference xmlref = iterator->first;
InterfacePtr<IIDXMLElement> element(xmlref.Instantiate());
XMLContentIterator iter(element->begin());
for(; iter != element->end(); iter++)
{
InDesign::TextRange range = *iter;
if(range.Length()>0)
{
do
{
InterfacePtr<ITextModel> textModel(range.QueryModel());
TextIterator begin(textModel, range.Start());
TextIterator end(textModel, range.End());
for (TextIterator iterr = begin; iterr != end; iterr++)
{
const UTF32TextChar characterCode = *iterr;
wstr.Append(characterCode);
}
}
while(false);
}
}
for(int i=0;i<element->GetChildCount();i++)
{
XMLReference childRef = element->GetNthChild(i);
InterfacePtr<IIDXMLElement> childelement(childRef.Instantiate());
XMLContentIterator iter(childelement->begin());
for(; iter != childelement->end(); iter++)
{
InDesign::TextRange range = *iter;
if(range.Length()>0)
{
do
{
InterfacePtr<ITextModel> textModel(range.QueryModel());
TextIterator begin(textModel, range.Start());
TextIterator end(textModel, range.End());
for (TextIterator iterr = begin; iterr != end; iterr++)
{
const UTF32TextChar characterCode = *iterr;
wstr.Append(characterCode);
}
}
while(false);
}
}
}
PMString str(wstr);
CAlert::InformationAlert(str);
iterator++;
}
Regards,
Chinna
Copy link to clipboard
Copied
Maybe there's a problem with iterators direction?
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more