regEx is not working while parsing through xsl - regex

hi i want to put a regex for my xml tag the tag is start with <SV1> here i want to put regEx for 1 . it can be any number from 0 to 1 . it is not working
i have tryed with below code.
<LineAmount><xsl:value-of select="SV*.[0-9].*/SV102"/></LineAmount>
it is not woking can some body help me.

If you want to use regular expression matching in XSLT then you need to use at least XSLT 2.0 (like for instance with an XSLT 2.0 processor like Saxon 9 or XmlPrime) and for element names (or node names in general) you need to use a predicate that tests the name e.g.
<xsl:value-of select="*[matches(local-name(), 'SV[0-9]')]/SV102"/>
selects SV102 with a parent of the name SV0, SV1, SV2, SV3 and so on.

Related

XSLT 2 analyze-string finding block names

I am trying to detect strings in other languages in my XML.
I thought I could use something like :
<xsl:analyze-string select="$mystring" regex="(\p{InGreek})" >
but I am unable to make this work.
Do you think this is possible in XSLT ? How would you do this ?
Thanks a lot.
Maria
(XSLT 2, Saxon-HE 9.8.0.8)
I think the right category name would be IsGreek so the regular expression would be \p{IsGreek}, however as the regex attribute of xsl:analyze-string allows attribute value templates you either need to put the expression into a string variable <xsl:param name="pattern" as="xs:string">\p{IsGreek}</xsl:param>you reference as regex="{$pattern}" or you need to duplicate the curly braces, as in regex="\p{{IsGreek}}".

Remove exact tag, whether with closing or self-closing, and attributes in PHP [duplicate]

I want to use the dom removeChild function in php to remove everything between a tag.
my xml looks like
<root>
<element>text</element>
<remove>
text
<morexml>text</morexml>
</remove>
</root>
Now I want to remove the tag including its entire inside. How do I do this? I do not have a clue. I am trying to use the only dom function i found: removeChild.
When removed it has to look like this:
<root>
<element>text</element>
</root>
Is there a php dom function to do this? I can not find it on google or stackoverflow.
$dom = new DOMDocument;
$dom->loadXML($xml);
$dom->getElementsByTagName('root')->item(0)
->removeChild($dom->getElementsByTagName('remove')->item(0));
This is very specific, though. You can use XPath if you need more generality:
foreach ($xpath->query('//remove') as $node) {
$node->parentNode->removeChild($node);
}
You can use XPath and delete over XPath the node.
Use DOM and XPath to remove a node from a sitemap file
PHP SimpleXML - Remove xpath node
here on Stackoverflow are a lot of posts. Perhaps you should search here at first.

RegEx for mining XML tag content

Fellow Forum Members,
I am using the latest NotePad++. I have 430 separate XML files and my goal is to make a "dmcode" list of all 430 XML files. The dmcode identifies each XML file and looks like the example code shown below. I need help in developing a Regular Expression that will grab the dmcode tag content located between the <dmCode opening tag and the closing /> terminator. Also I only need this extraction to only apply to dmcode tags that follows the <dmIdent> tag. In other words, any dmcode tag that is not preceded by a <dmIdent> tag does not end up on my NotePad++ search result list. Is such a Regular Expression that can pull targeted data from a lot of XML files possible?
<dmIdent>
<dmCode assyCode="00" disassyCode="00" disassyCodeVariant="00" infoCode="042" infoCodeVariant="A" itemLocationCode="O" modelIdentCode="SASA" subSubSystemCode="6" subSystemCode="0" systemCode="A03" systemDiffCode="XY"/>
As an alternative I have been researching using an XPath expression to accomplish the same task. However, I can't seem to find a NotePad++ XPath plugin that will enable me to specify the data I want to extract from 430 XML files by using an XPath expression instead of a Regular Expression. I will also appreciate it if anyone can provide an example of an XPath expression that will perform the same task I'm trying to accomplish by using a Regular Expression.
Any help will be greatly appreciated.
I know there are plugins for XPath, but I don't know one that allows you to search several files. The following XPath would match all attributes in <dmCode> as a child of the root element <dmIdent>:
/dmIdent/dmCode[#*]
I need help in developing a Regular Expression that will grab the dmcode tag content located between the <dmCode opening tag and the closing /> terminator. Also I only need this extraction to only apply to dmCode tags that follow the <dmIdent> tag.
This will work for the most simple cases, where:
<dmCode> is the first child of <dmIdent>
There are no comments, CDATA tags, or similar constructs that could make it fail.
(?i)<dmIdent>\s*<dmCode \K[^"/>]*(?>(?:"[^\\"]*(?:\\.[^\\"]*)*"|/(?!>))[^"/>]*)*(?=/>)
regex101 demo
Matches:
(?i)<dmIdent>\s*<dmCode both tags spearated by whitespace (case-insensitively)
\K resets the matched text
[^"/>]* Any characters except ", / or >
And loops:
"[^\\"]*(?:\\.[^\\"]*)*" text in quotes, or
/(?!>) a / not followed by >
both followed by the previous [^"/>]*
(?=/>) All followed by />

xml Regex matching the whole xml file

I need a regular expression that given the following XML, will give me all the products (productos) that have 'Bebidas' as a category (categoria), and I have to do this in Sublime Text, so only have the option to use a regular expression (no dedicated XML parser allowed):
XML File www.ethgf.com/electricos.xml
I have a problem when I use (?s)<producto>(.+?Bebidas.+?)<\/producto> because it highlights almost all the XML (the first 'producto' tag through the last tag closure).
Since the question is about selecting the whole <product> nodes, you can use the following regex:
(?s)<product>(?:\s*<(\w+)>[^<]*?<\/\1>\s*)*?\s*<category>Drinks<\/category>(?:\s*<(\w+)>[^<]*?<\/\2>\s*)*?\s*<\/product>
It will highlight all <product> nodes that contain Drinks category, even if the nodes are not following some strict order:

How to find a word within text using XSLT 2.0 and REGEX (which doesn't have \b word boundary)?

I am attempting to scan a string of words and look for the presence of a particular word(case insensitive) in an XSLT 2.0 stylesheet using REGEX.
I have a list of words that I wish to iterate over and determine whether or not they exist within a given string.
I want to match on a word anywhere within the given text, but I do not want to match within a word (i.e. A search for foo should not match on "food" and a search for bar should not match on "rebar").
XSLT 2.0 REGEX does not have a word boundary(\b), so I need to replicate it as best I can.
You can use alternation to avoid repetition:
<xsl:if test="matches($prose, concat('(^|\W)', $word, '($|\W)'),'i')">
If your XSLT 2.0 processor is Saxon 9 then you can use Java regular expression syntax (including \b) with the functions matches, tokenize and replace by starting the flag attribute with an exclamation mark:
<xsl:value-of select="matches('all foo is bar', '\bfoo\b', '!i')"/>
Michael Kay mentioned that option recently on the XSL mailing list.