Given xml of
$xml = [Xml]#"
<Package>
<Copy_Ex>
</Copy_Ex>
<Move_Ex>
<Rules>
<Rule>
<Property>os</Property>
<Operator>-eq</Operator>
<Value>Windows10</Value>
</Rule>
</Rules>
<Task>
<Rules>
<Rule>
<Property>lastWriteTime</Property>
<Operator>-gt</Operator>
<Value>6W</Value>
</Rule>
</Rules>
</Task>
</Move_Ex>
</Package>
"#
I want to be able to select a single node from the xml, say <Move_Ex>, and pass it to a function, then independently access the two <Rules> nodes. I can do that now with
function Test {
param (
[Xml.XmlElement]$task
)
if ($nodeRules = $task.SelectSingleNode('//Rules')) {
Write-PxXmlToConsole $nodeRules
}
if ($taskRules = $task.SelectSingleNode('//Task/Rules')) {
Write-PxXmlToConsole $taskRules
}
}
$task = $xml.SelectSingleNode('//Move_Ex')
Test $task
But // is searching the entire document, NOT just the [Xml.XmlElement] that I passed. So if I have a different <Rules> node in the <Copy_Ex> node, THAT is what gets returned by the first SelectSingleNode(). What I think I need is a way to identify the root node of the passed element, not the entire document. But I can't seem to find a way to do that consistently. My understanding is that while // finds the sequence of nodes anywhere in the document, / only finds it relative to the root node. Which should mean that
$task = $xml.SelectSingleNode('/Move_Ex')
finds only that <Move_Ex> in the root, and if I had another one somewhere else I would be fine. However, that doesn't return anything at all, which has me worried I don't really understand how either / or // works, which makes the chances of getting what I need to work unlikely.
I have looked at the documentation for [Xml.XmlElement] and GetElementsByTagName() seems to be finding just the elements in my passed element. Write-Host "$($task.GetElementsByTagName('Rules').Count)" in the function returns a 2, so not finding any <Rules> node I have put in <Copy_Ex>.
I also tried just dot referencing things, so
Write-PxXmlToConsole $task.Rules
Write-PxXmlToConsole $task.Task.Rules
And that seems to be working also. But again, ONLY when the element being passed is selected with // rather than /, and I worry that in a much more complex XML with hundreds of <Rules> nodes I won't be getting consistent results.
So, two questions...
1: What is the difference between / and // in this situation, and why isn't /Move_Ex working as "expected" in $task = $xml.SelectSingleNode('/Move_Ex')?
2: Is the dot referencing approach the "correct" way to limit my access to just the passed Element? Or is there another/better way?
I should note here that I did verify that // does break down if make the XML more realistic. So with this XML
$xml = [Xml]#"
<Package>
<Copy_Ex>
<Rules>
<Rule>
<Property>os</Property>
<Operator>-eq</Operator>
<Value>WindowsXP</Value>
</Rule>
</Rules>
<PreTask>
<Move_Ex>
<Rules>
<Rule>
<Property>os</Property>
<Operator>-eq</Operator>
<Value>WindowsVista</Value>
</Rule>
</Rules>
<Task>
<Rules>
<Rule>
<Property>lastWriteTime</Property>
<Operator>-gt</Operator>
<Value>8W</Value>
</Rule>
</Rules>
</Task>
</Move_Ex>
</PreTask>
</Copy_Ex>
<Move_Ex>
<Rules>
<Rule>
<Property>os</Property>
<Operator>-eq</Operator>
<Value>Windows10</Value>
</Rule>
</Rules>
<Task>
<Rules>
<Rule>
<Property>lastWriteTime</Property>
<Operator>-gt</Operator>
<Value>6W</Value>
</Rule>
</Rules>
</Task>
</Move_Ex>
</Package>
"#
Where I need to select only the <Move_Ex> in the <Package> node
$task = $xml.SelectSingleNode('/Move_Ex')
selects nothing, while
$task = $xml.SelectSingleNode('//Move_Ex')
selects the <Move_Ex> node nested in <Copy_Ex>, which is not at all what I want and need.
Note that all Write-PxXmlToConsole does is exactly that.
function Write-PxXmlToConsole ($xml) {
$stringWriter = New-Object System.IO.StringWriter
$xmlWriter = New-Object System.Xml.XmlTextWriter $stringWriter
$xmlWriter.Formatting = "indented"
$xml.WriteTo($xmlWriter)
$xmlWriter.Flush()
$stringWriter.Flush()
Write-Host $stringWriter.ToString()
Write-Host
}
EDIT: With respect to my first question, and based on what #Prophet has already said, I realized I was conflating Root NODE and Root ELEMENT, and now I see this in my related links. Some good additional info about Root node, root element, document element, etc.
Maybe I'm missing something or misunderstanding your question, but it seems to me that the answer is very simple:
The difference between / and //:
/ will go to the direct child of the passed argument while // will look for any element below the passed argument.
By default XPath searches starting from the root node. This is why '/Move_Ex' returns nothing. There is no Move_Ex element directly below the root.
To start searching from current node, not from the root, you should put a dot . at the prefix of the XPath expression.
So, to get the Rules nodes inside the passed element you should use this XPath: '//.Rules'
Related
If I put "<commentGenerator>" after "<jdbcConnection>", MBG proposed an error that context content should match: blablabla...
But when I put "<commentGenerator>" before "<jdbcConnection>", everything is ok. Here I have something to complain to the official website that if the order of these items is need, why you do not tell us! What an important thing! You r kidding the freshmen. Maybe it is some where I do not know, but this is a key point to build the MBG's configuration file successfully, why not put this NOTE on the top of the tutorial or somewhere eye-catching?
<generatorConfiguration >
<classPathEntry location="D:\mariadb-java-client-1.1.7.jar" />
<context id="db" >
<commentGenerator>
<property name="suppressAllComments" value="true" />
<property name="suppressDate" value="true" />
</commentGenerator>
<jdbcConnection driverClass="org.mariadb.jdbc.Driver"
connectionURL="jdbc:mariadb://localhost:3306/dbname"
userId="root"
password="password"
/>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- Model Class -->
<javaModelGenerator targetPackage="org.infrastructure.model" targetProject="infrastructure\src\main\java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- XML Files -->
<sqlMapGenerator targetPackage="sqlMap" targetProject="infrastructure\src\main\config">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- DAO -->
<javaClientGenerator type="XMLMAPPER" targetPackage="org.infrastructure.dao" targetProject="infrastructure\src\main\java">
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- Tables -->
<table tableName="user" domainObjectName="User" ></table>
</context>
</generatorConfiguration>
First of all, in your xml configuration file, it doesn't contains a valid root element, which always should be like <!DOCTYPE .../>. About how to add a correct root element of mybatis generator configuration file, please see example from MyBatis GeneratorXML Configuration File Reference.
If you correctly specified root element such as following:
<!DOCTYPE generatorConfiguration PUBLIC
"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"
>
This root element contains a typical DTD declaration located at http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd. This is the definition of the order of these items is need. And we are going to see what it looks like.
From line 47 of this document type definition, it defines element named context. The content is as following:
<!--
The context element is used to describe a context for generating files, and the source tables.
-->
<!ELEMENT context (property*, plugin*, commentGenerator?, jdbcConnection, javaTypeResolver?,javaModelGenerator, sqlMapGenerator?, javaClientGenerator?, table+)>
Which obviously defined the order of the element in context, that is:
property*, plugin*, commentGenerator?, jdbcConnection,
javaTypeResolver?,javaModelGenerator, sqlMapGenerator?,
javaClientGenerator?, table+
In this element, all children must occurs as following rules:
+ for specifying that there must be one or more occurrences of the item ā the effective content of each occurrence may be different;
* for specifying that any number (zero or more) of occurrences is allowed ā the item is optional and the effective content of each occurrence may be different;
? for specifying that there must not be more than one occurrence ā the item is optional;
If there is no quantifier, the specified item must occur exactly one time at the specified position in the content of the element.
After we understanding its real meaning, why you could not change the order of commentGenerator and jdbcConnection should be clear.
Maybe you want to know how to make the element out of order, question How to define DTD without strict element order could be useful.
Wish it helpful.
Hi Iām having problem understand why I get different response when I use the (org.xacmlinfo.xacml.pep.agent.PEPAgent.java) and the tryit function in WSO2IS 5.0.0. They are querying the same policy.
<Policy xmlns="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17" PolicyId="OfficeHours" RuleCombiningAlgId="urn:oasis:names:tc:xacml:1.0:rule-combining-algorithm:permit-overrides" Version="1.0">
<Description>denyOutsideOfficeHours</Description>
<Target>
<AnyOf>
<AllOf>
<Match MatchId="urn:oasis:names:tc:xacml:1.0:function:string-equal">
<AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">foo.com</AttributeValue>
<AttributeDesignator AttributeId="urn:oasis:names:tc:xacml:1.0:environment:environment-id" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:environment" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="true"></AttributeDesignator>
</Match>
</AllOf>
</AnyOf>
</Target>
<Rule Effect="Permit" RuleId="PermitInOfficeHours">
<Condition>
<Apply FunctionId="urn:oasis:names:tc:xacml:2.0:function:time-in-range">
<AttributeValue DataType="http://www.w3.org/2001/XMLSchema#time">12:00:00</AttributeValue>
<AttributeValue DataType="http://www.w3.org/2001/XMLSchema#time">17:00:00</AttributeValue>
<Apply FunctionId="urn:oasis:names:tc:xacml:1.0:function:time-one-and-only">
<AttributeDesignator MustBePresent="false" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:environment" AttributeId="urn:oasis:names:tc:xacml:1.0:environment:current-time" DataType="http://www.w3.org/2001/XMLSchema#time"></AttributeDesignator>
</Apply>
</Apply>
</Condition>
</Rule>
</Policy>
PEPAgent
<Request xmlns="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17" CombinedDecision="false" ReturnPolicyIdList="false">
<Attributes Category="urn:oasis:names:tc:xacml:3.0:attribute-category:environment">
<Attribute AttributeId="urn:oasis:names:tc:xacml:1.0:environment:environment-id" IncludeInResult="false"><AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">foo.com</AttributeValue>
</Attribute>
</Attributes>
</Request>
<Response xmlns="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17">
<Result>
<Decision>Indeterminate</Decision><Status>
<StatusCode Value="urn:oasis:names:tc:xacml:1.0:status:processing-error"/>
<StatusMessage>urn:oasis:names:tc:xacml:1.0:function:time-one-and-only expects a bag that contains a single element, got a bag with 0 elements</StatusMessage>
</Status>
</Result>
</Response>
Tryit
<Request xmlns="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17" CombinedDecision="false" ReturnPolicyIdList="false">
<Attributes Category="urn:oasis:names:tc:xacml:3.0:attribute-category:environment">
<Attribute AttributeId="urn:oasis:names:tc:xacml:1.0:environment:environment-id" IncludeInResult="false">
<AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">foo.com</AttributeValue>
</Attribute></Attributes></Request>
<Response xmlns="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17">
<Result><Decision>Permit</Decision><Status>
<StatusCode Value="urn:oasis:names:tc:xacml:1.0:status:ok"/>
</Status><PolicyIdentifierList>
<PolicyIdReference>OfficeHours</PolicyIdReference>
</PolicyIdentifierList>
</Result>
</Response>
First of all here are a few comments regarding your policy and rule:
the policy description doesn't match the rule description. In one case you say deny, in the other you say Permit.
you use a Condition where in fact a Target would be enough.
Now, as for the different responses: in one case your code (PEPAgent) doesn't send the current time value and the server (is that the PDP?) doesn't add the time either. In the second case, the Tryit client still doesn't send time but obviously the receiving end (another PDP?) fills in the missing time value.
If you look more closely at your first response, you will notice the following error message:
<StatusMessage>urn:oasis:names:tc:xacml:1.0:function:time-one-and-only expects a bag that contains a single element, got a bag with 0 elements</StatusMessage>
This means that you didn't pass in any value for current-time.
I tested this using the Axiomatics Policy Server and I received the expected behavior.
I have a web.config file in which I need to either insert the <configSections /> element or manipulate children of that node if it already exists.
If it already exists I don't want to insert it again (obviously, as it is only allowed to exist once).
Normally, that would not be a problem, however:
If this element is in a configuration file, it must be the first child element of the element.
Source: MSDN.
So if I use xdt:Transform="InsertIfMissing" the <configSections /> element will always be inserted after any existing child elements (and there are always some), violating the above restriction of it having to be the first child element of <configuration />
I attempted to make this work in the following way:
<configSections
xdt:Transform="InsertBefore(/configuration/*[1])"
xdt:Locator="Condition(not(.))" />
Which works perfect, if the <configSections /> element doesn't already exist. However, the condition I've specified seems to be ignored.
In fact, I've tried a few conditions like:
Condition(not(/configuration[configSections]))
Condition(/configuration[configSections] = false())
Condition(not(/configuration/configSections))
Condition(/configuration/configSections = false())
Finally, out of desperation, I tried:
Condition(true() = false())
It still inserted the <configSections /> element.
It is important to note that I'm trying to include this in a NuGet package, so I will be unable to use a custom transform (like the one AppHarbor uses).
Is there any other clever way to get my element in the right place only if it doesn't yet exist?
To test this out, use AppHarbors config transform tester. Replace the Web.config with the following:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="initialSection" />
</configSections>
</configuration>
And Web.Debug.config with the following:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<configSections
xdt:Transform="InsertBefore(/configuration/*[1])"
xdt:Locator="Condition(true() = false())" />
<configSections>
<section name="mySection" xdt:Transform="Insert" />
</configSections>
</configuration>
The result will show two <configSections /> elements, the one containing "mySection" being the first, as specified in the InsertBefore Transform.
Why was the Locator Condition not taken into account?
So after facing the same issue, I came up with a solution. It's not pretty nor elegant, but it works. (At least on my machine)
I just split the logic into 3 different statements. First, I add an empty configSections at the correct position (first). Then I insert the new config to the last configSections, which would be the new one if it is the only one, or a previously existing one otherwise.
Lastly I remove any empty configSections elemnt which might exist. I'm using RemoveAll for no good reason, you should probably use Remove.
The overall code looks like so:
<configSections xdt:Transform="InsertBefore(/configuration/*[1])" />
<configSections xdt:Locator="XPath(/configuration/configSections[last()])">
<section name="initialSection" xdt:Locator="Match(name)" xdt:Transform="InsertIfMissing" />
</configSections>
<configSections xdt:Transform="RemoveAll" xdt:Locator="Condition(count(*)=0)" />
The question which still remains unanswered is why Locator conditions are not taken into account for InsertBefore. Or why I can't handle an empty match set for InsertBefore, because that would allowed me to do fun things such as
//configuration/*[position()=1 and not(local-name()='configSections')]
Which to be honest is a much clearer way of doing what I want to achieve.
e.g.:
<rule>
<from>^/create#name=(.*)$</from>
<to>/page/new/success.jsp?name=$1</to>
</rule>
when I enter the uri like "/create#name=elvis",it cannot rewrite and forward to new address,
how can I do now , how to transcoding the sign '#'
???
Does something like this work (escaping the # sign)?
Adding type Forward:
<rule>
<from>/create\#name=(.*)</from>
<to type="forward">/page/new/success.jsp?name=$1</to>
</rule>
Adding type Redirect:
<rule>
<from>/create\#name=(.*)</from>
<to type="redirect">/page/new/success.jsp?name=$1</to>
</rule>
I have to rewrite the core cache model. And this doesn't work. My first attempt to solve the problem was to try the rewrite with another model...
In my config.xml I declared the following
<global>
<models>
<core>
<rewrite>
<**layout**>MyCompany_MyModule_Model_Core_Cache</**layout**>
</rewrite>
</core>
</models>
....
and in my class I died in the consturctor.
This works perfectly!
So my may in rewriting models is the right one.
But If I don't use the layout-node in the xml but using the cache-node instead this does not work.
So my attempt is the following and this is not working:
<global>
<models>
<core>
<rewrite>
<cache>MyCompany_MyModule_Model_Core_Cache</cache>
</rewrite>
</core>
</models>
....
My question now: is there a way to rewrite / overload the "cache-core-model"???
The cache will be initialized before module configurations (config.xml) are loaded. The cache-Model was instanciated with Mage::getModel, which caches the modelnames in the registry.
So all later tries to get the custom cache-model will also fail.
Solution: put this rewrite statement in the etc/local.xml. This is a bit dirty because the local.xml should only hold module independent stuff. But this is better than copying core files to local.
I had the same question, but my solution is a little bit different to yours ;-)
Magento will load the XML file from /app/etc/*.xml (this files will not be cached) before everything else in magento.
So i created my own file here "cache.xml" and the content is
<?xml version="1.0"?>
<config>
<global>
<models>
<core>
<rewrite>
<cache>MyCompany_MyModule_Model_Core_Cache</cache>
</rewrite>
</core>
</models>
</global>
</config>
works perfect in 1.6,1.7 and 1.8
I'm also trying to do the same thing and i don't think it's possible. If you var_dump out $this->_xml->group->models in the method: getGroupedClassName (app/code/core/Mage/Core/Model/Config.php) you'll notice that your rewrite it's not yet available, hence why it's skipped.
If you try to overwrite translate or layout: your_class_model you'll notice that the $this->_xml... dumps the initial core classes (with no rewrite) and you'll see your rewrite down the line well past the core/cache. So, it's probably being overridden but the class is already instantiated, set up and used; so it's really not going to fire anything.
Hence i don't think it's possible to override the core/cache. You'll have to move it to the app/code/local. Pitty.